• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginning Method to stop the program when an even number is entered.  RSS feed

 
Gus Flores
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jordan Young
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


I'd suggest googling the for loop if you don't know the exact syntax for it. The habit of googling things is what I'd consider a skill among the programmer's arsenal.

Just this once though, I shall show you how to do the for loop.

for(int i = 0; i < someNumber; i++) {
<enter code here>
<enter exit code by setting i to a number greater than someNumber>
}

Happy coding!
 
Gus Flores
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I understand the for loop syntax but i dont understand how to end it for even odd numbers using the for loop.
 
Jordan Young
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was about to suggest a method myself, but I would change that a bit.
  • 1: I would put that in a different class. Let's call it MyUtilities but you can probably think of a different name.
  • 2: That method only ever takes in information via its parameter and only sends back information via its return type, not to or from anywhere else, so it is 1368 in the most dubious classification of methods known to modern science, so you can mark it static.
  • 3: If all the methods are static, you never need an instance. The Java® Language Specification tells you to use a private constructor if you don't need instances.
  • 4: If the method is in a different class, then private access isn't appropriate.
  • You will see the actual body of the method is the same as Liutauras Vilda showed earlier.Beware: Advanced Topic Follows
    You don't have to do what follows if you don't want to.
    If you have a half‑hour to spare, work out why you can change line 9 to read
    return (i & 1) == 0;
    Does that make any difference if you pass a negative argument?
    Does that have faster or slower execution than %?
    Does it work for other values to the right of the & than 1?
    Can you understand it better?
    Are you wasting your time looking at such advanced things?
     
    Gus Flores
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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


    for my homework assignment i'm not allowed to use anything else but the method
    getEvenValue ()
    {
    System.out.println("Enter a number"):
    }

    an if/ else with a loop to stop the program when an even number is entered.
     
    Gus Flores
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.


    well i'm confused where to put the for loop at. i have the if else statement down but when i put the for loop i put (val = 0; val !=0; val++) and it stops the program only on the first even number recorded not on any numbers after. so it will ask for another number it gets an odd and but when an even number is entered after the first couple it doesn't stop.
     
    Campbell Ritchie
    Marshal
    Posts: 56522
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Gus Flores
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.


    yeah. since its a beginning java 1 class were not allowed to use anything else that he suggest. that way we understand it better step by step. but it seems to make it harder lol
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!