# Help loop to decide even or odd number !?

• 06-21-2013, 11:56 AM
Help loop to decide even or odd number !?
Hello,

am a newbie to java and just started learning methods, didnt master loops yet and i am trying to figure out how to solve a questions i stumbled on but couldnt really got an idea on how to do it, i need to write a program asking the user to enter a number an even number, then the program should decide if it is an even or odd number, if it is an even number just output the number ( system.out.println(number)) simply but if the number is odd the program should as the user to reenter an input which is an even number, so it should be through a loop till the number is even ....

i hope someone could show me how ?

where i got is :

Quote:

import java.util.Scanner;
public class excrs {
public static void main(String[] args) {

Scanner inpt = new Scanner(System.in);
System.out.println("Enter an Even number");
int i = inpt.nextInt();

while(i%2 != 0){

System.out.println("The number you have entered is not an even number, enter another number");

}

}
}

======

now am stuck because i got confused on how to keep the loop from going depending on a user enter not through a a range like ( 1<50) then i++

and how to exit the loop ?

actually i am even trying to make the loop or the decision making of the odd or even in a different method, which i know that i have to creat another public class int name( para ), but i thought of doing it all in one method then devide it ...

• 06-21-2013, 12:54 PM
PhHein
Re: Help loop to decide even or odd number !?

For this you normally use a do while loop. Pseudocode:
do
print: enter even number
while input not even
• 06-21-2013, 04:18 PM
Re: Help loop to decide even or odd number !?

i understand your instructions, and thats the general idea, i already got that in mind, the thing is i coudnt apply that idea with a right loop construction, if you could be more detailed please ...
• 06-21-2013, 04:24 PM
PhHein
Re: Help loop to decide even or odd number !?
• 06-21-2013, 09:57 PM
Re: Help loop to decide even or odd number !?
Appreciate the link, it gave me a good hint and cleared somethings to me as i could so it kind of guided toward the right direction but there is something am doing wrong but not sure what ....

Code:

```import java.util.Scanner; public class excrs { public static void main(String[] args) {         Scanner inpt = new Scanner(System.in); System.out.println("Enter a number"); int i = inpt.nextInt(); int decision = oddeven; System.out.println(oddeven); } public static int oddeven(int I){         if (I%2==0){                 do {                                 System.out.println(+ I +" is not an even number, enter another number");                                 Scanner INPT = new Scanner(System.in);                 int num =INPT.nextInt();                                         while(INPT%2 != 0);                         int even =inpt.nextInt();                                                 return oddeven;                         }                                                 } }```
any idea !?
• 06-22-2013, 03:22 AM
Junky
Re: Help loop to decide even or odd number !?
When your code has a problem don't just dump it on the forum and say "what's wrong". Provide us with some details. Does it compile? If not post full and exact error messages. Does it give incorrect output? Then post a sample run of the program with inputs and outputs and explain exactly where it goes wrong.

I can see your code does not follow the pseudocode provided. In the oddeven method you have an if statement to test if the value is even, if it is even it enters the if statement and the do while loop and immediately prints "not an even number" Hmmmmm!
• 06-22-2013, 03:30 AM
DarrylBurke
Re: Help loop to decide even or odd number !?
• 06-22-2013, 10:46 AM