Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to alternate plus and minus signs in java?

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to alternate plus and minus signs in java?

    So my code is:

    class series {
    	public static void main(String args[]){
    		int numerator, denominator;
     
    		numerator = 1;
    		denominator = 1;
     
    		System.out.print(numerator);
     
    		for(int counter=0;counter<=198;counter++){
    			denominator+=1;
     
    			System.out.print(" + "+numerator+"/"+denominator+" ");
     
    		}
     
     
    	}
    }

    So far it prints out this:
    1 + 1/2 + 1/3 + 1/4 + 1/5...
    but I want it to print out like this:
    1 - 1/2 + 1/3 - 1/4 + 1/5...

    How would I do it?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to alternate plus and minus signs in java?

    The easiest way? Use mod. An integer mod 2 will result in either 0 or 1 (depending on whether the number is odd or even). Then you just need an if statement where:
    if(someNumber % 2 == 0)
        print plus sign
    else
        print minus sign
    Or whichever way you want. You get the idea.

    **Btw, "mod" is indicated by %
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to alternate plus and minus signs in java?

    class series {
    	public static void main(String args[]){
    		int numerator, denominator;
     
    		numerator = 1;
    		denominator = 1;
     
    		System.out.print(numerator);
     
    		for(int counter=0;counter<=198;counter++){
    			denominator+=1;
    			System.out.print(" + "+numerator+"/"+denominator+" ");
    			if(numerator % 2 == 0){
    				System.out.print("+");
    			}else{
    				System.out.print("-");
    			}
    		}
     
     
    	}
    }

    But now the output is wrong:
    1 + 1/2 - + 1/3 - + 1/4 - + 1/5...

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to alternate plus and minus signs in java?

    Well, first of all, you want to take out where you add the plus symbol in your first print statement (the one where you print the numerator and the denominator). Second, your numerator doesn't change, so that is not the number you want to mod. Instead, you want to mod either the counter or the denominator (whichever makes more sense for your problem). Lastly, you want the if/else statement before you print the numerator and denominator (since the symbol needs to come before the fraction).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to alternate plus and minus signs in java?

    Ok now I have to add all those numbers together, for e.g., the sum of 1 - 1/2 + 1/3 - 1/4 + 1/5 = 0.783

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to alternate plus and minus signs in java?

    Oh. That's a bit more complicated. You will want a double variable located outside of your for loop. Inside your if/else statement, you would want to add or subtract the value of: numerator/denominator by the double variable outside of your for loop. This value is your total.
    There is one problem with this however: numerator and denominator are both ints. So when you divide them, the result with be an int (meaning it will not contain a decimal). That's a big problem. You want your division to be a double, not an int. We can get a result to be a double only if at least one of the values are doubles. There are two ways of handling this:
    1. Multiply either numerator or denominator by 1.0 before dividing them. Multiplying one of them before dividing will result in you using the value of the int times 1.0 in the division operation. By multiplying by 1.0, you are converting that value into a double, which will yield a double value after dividing. This would look like this: ((1.0*numerator)/denominator)
    Note that the decimal and the 0 in the tens place is critical. Multiplying by just 1 (no decimal) will NOT result in a double.
    2. You could change either numerator or denominator to be a double instead of an int. While this would not require you to do #1's multiplication, it will result in a .0 getting printed out by your print statement. This can be fixed by casting the value to an int prior to printing it:
    System.out.println(((int)numerator)+"/"+denominator); // This example assumes numerator was changed to a double
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to alternate plus and minus signs in java?

    Quote Originally Posted by aussiemcgr View Post
    Oh. That's a bit more complicated. You will want a double variable located outside of your for loop. Inside your if/else statement, you would want to add or subtract the value of: numerator/denominator by the double variable outside of your for loop. This value is your total.
    There is one problem with this however: numerator and denominator are both ints. So when you divide them, the result with be an int (meaning it will not contain a decimal). That's a big problem. You want your division to be a double, not an int. We can get a result to be a double only if at least one of the values are doubles. There are two ways of handling this:
    1. Multiply either numerator or denominator by 1.0 before dividing them. Multiplying one of them before dividing will result in you using the value of the int times 1.0 in the division operation. By multiplying by 1.0, you are converting that value into a double, which will yield a double value after dividing. This would look like this: ((1.0*numerator)/denominator)
    Note that the decimal and the 0 in the tens place is critical. Multiplying by just 1 (no decimal) will NOT result in a double.
    2. You could change either numerator or denominator to be a double instead of an int. While this would not require you to do #1's multiplication, it will result in a .0 getting printed out by your print statement. This can be fixed by casting the value to an int prior to printing it:
    System.out.println(((int)numerator)+"/"+denominator); // This example assumes numerator was changed to a double
    Now could you put that in English? I'm only a beginner in java.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to alternate plus and minus signs in java?

    Have you got the if statement working to alternate between + and -? If so add further code in each branch to perform the different operations.
    Improving the world one idiot at a time!

Similar Threads

  1. Alternate Decision Process Needed
    By brooksr13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: October 12th, 2013, 04:27 AM
  2. Alternate turns using Polymorphism and abstract classes
    By dalizardking427 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 13th, 2013, 07:48 PM
  3. Java Game - Alternate between players
    By JohnQ in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 21st, 2011, 08:49 PM
  4. How to replace dollar signs with ""?
    By colerelm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2011, 07:45 AM