Results 1 to 8 of 8
  1. #1
    madnomad is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default 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 :

    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 ...

    Help please
    Last edited by madnomad; 06-21-2013 at 05:13 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Help loop to decide even or odd number !?

    Please wrap your code in code tags.

    For this you normally use a do while loop. Pseudocode:
    do
    print: enter even number
    read input
    while input not even
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    madnomad is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Help loop to decide even or odd number !?

    Thanx for the fast reply

    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 ...

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Help loop to decide even or odd number !?

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    madnomad is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default 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 ....

    Java 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 !?
    Last edited by madnomad; 06-22-2013 at 12:00 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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!

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Help loop to decide even or odd number !?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    madnomad is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Help loop to decide even or odd number !?

    Thank you for the efforts i appreciate it, still struggling to find the solution this problem, but i have a feeling i am making it complicated when it is simple.

    Thanks anyhow

Similar Threads

  1. To decide M.Tech Project
    By sakshi.13agg in forum Forum Lobby
    Replies: 1
    Last Post: 10-15-2012, 03:57 AM
  2. Help (Java Eclipse loop using inputted number)
    By micolord in forum New To Java
    Replies: 12
    Last Post: 09-11-2011, 02:23 PM
  3. Help (Java Eclipse loop using inputted number)
    By micolord in forum Eclipse
    Replies: 1
    Last Post: 09-07-2011, 03:45 PM
  4. Generate Random Number Loop
    By saber210 in forum New To Java
    Replies: 9
    Last Post: 08-12-2011, 05:49 PM
  5. Loop through all letter and number possibilities?
    By Mr.abe90 in forum New To Java
    Replies: 11
    Last Post: 05-29-2011, 08:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •