Gus Flores wrote:having trouble with making the for loop to have my method to continue to ask user to input a number every time an odd number is entered. for the program to stop once an even number is entered. heres my code. i don't know how to write out the for loop. i'm only allowed to use if/else with a for loop. If anyone would mind adding me to skype or kik messenger as well for future help. I"m not looking for answers since I do want to learn. but it is hard since i've never programmed before.
Gus Flores wrote:Oh I understand the for loop syntax but i dont understand how to end it for even odd numbers using the for loop.
Liutauras Vilda wrote:Hi Gus Flores,
How would you do it in English? On a piece of paper?
Maybe "do-while" loop would fit best here?
1. Ask user enter the number (do)
2. Check if it is odd or even (body)
3. Ask again, if 2 condition doesn't satisfy you (while)
In order to check if it is odd or even, would be nice to have a method? Partially you got that, and it should look similar to:
Welcome to JavaRanch
Jordan Young wrote:
Gus Flores wrote:Oh I understand the for loop syntax but i dont understand how to end it for even odd numbers using the for loop.
In your code posted above, it isn't wrapped in a for loop. To exit a loop you must set the variable being looped to break in the if statement once it is met. So in the example above when you check for an even number you can set the integer being looped to 9999 or something.
Campbell Ritchie wrote:You mean you are not allowed to write your own isEven method? Ask about that.
You can always copy the guts of the isEven method into the () after while, but I would have thought an isEven method is better design.