Hey, guys! I'm new to the forums and to java. I need help with what to do next on my code.
My instructions are to:
1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be
smaller than secondNum).
2. Output all the even numbers between firstNum and secondNum inclusive.
3. Output the sum of all the even numbers between firstNum and secondNum inclusive.
4. Output all the odd numbers between firstNum and secondNum inclusive.
5. Output the sum of all the odd numbers between firstNum and secondNum inclusive.
*Use while loop
I honestly don't understand what to do with the while loop and how to find even and odd numbers.
Ok, so I changed secondNum to firstNum and have entered an infinite loop. How do I define the even/odd integers between the two keyboard numbers I enter? Should make another while loop or add more if/else statements inside my loop?
My code now:
And if this help this is an example of how it should look like once I compile it:
Enter an integer:
Enter another integer larger than the first one:
Even numbers: 4 6 8 10
Sum of even numbers = 28
Odd numbers: 3 5 7 9
Sum of odd numbers = 24
I don't see anything in the instructions that says you can only use one while loop.
One key to better programs is to keep things simple. Do one thing at a time. The more you try to do at once, the more complicated things get. The more complicated things get, the harder it is for you to wrap your head around what you're really telling the computer to do. Bugs are simply a misunderstanding between you and the computer. They're a failure to communicate properly. You think you're telling it to do something but the instructions you wrote (the program) are telling it to do something else. So, while you're still learning how to communicate properly with the computer, keep your instructions simple so you know you're telling the computer to do exactly what you think you want it to do.
Next, a while loop will only terminate when the loop condition is false. If there's nothing inside the loop makes the loop condition false, then it will continue to repeat the instructions inside the loop. At some point, something needs to happen so that the condition of the while loop becomes false so it can stop repeating.
Pointdexter Huynh wrote:Ok, so I changed secondNum to firstNum and have entered an infinite loop.
That's not what he meant.
Your loop condition is (firstNum <= secondNum)
The value of one or both of these have to change at some point so that this expression is no longer true and the loop can terminate.
However, because the names are what they are, they're going to mess up your thought process in coming up with the correct logic. I suggest you use another variable to help control the loop, a variable whose name suggests that it will change throughout the loop execution. Try using currentNum instead, as in "the number to use in the current loop iteration". You want to say "While the number we're currently on is less than or equal to the second number the user entered, do these things: ..." And before entering the loop, you tell the computer "We're currently on the first number the user entered"
You could also use a name like x and think about it this way: Let x be a number that changes every time we execute the loop instructions. Starting with x equal to the first number the user entered, do blah blah blah. Then make x be the next number. Repeat this process if the new value of x is less than or equal to the second number the user entered, otherwise you're done.
Whether to use x or currentNum or some other name is a matter of preference. Note however, that variable names like "x" are not as expressive (they usually don't convey a clear idea) and should be used with care. See if you think this is more expressive:
Let currentNum be the number that we're currently using to execute the loop instructions. Starting with currentNum as the first number the user entered, do blah blah blah. Then make currentNum be the next number. Repeat this process if the new value of currentNum is less than or equal to the second number the user entered, otherwise you're done.