Results 1 to 9 of 9
  1. #1
    Naxix is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default While loop stops before told?

    Hello, so i'm new to java so the code isn't any special, i'm still learning the basics.
    But i was sitting messing around, when i got to this wierd thing, and i can't see why it happens,
    it might be me doing something wrong, if then i'm sorry to bother you.

    I made an example of the code here:
    Java Code:
    import java.util.Scanner;
    
    class Example {
    	public static void main(String args[]){
    		int Test;
    		Scanner Scan = new Scanner(System.in);
    		System.out.println("Guess the number: ");
    		Test = Scan.nextInt();
    		while(Test > 20){
    			Test = Scan.nextInt();
    			System.out.println("Too high, try again");
    		}
    		while(Test < 20){
    			System.out.println("Spot on!");
    		}
    	}
    }
    So the things i find wierd is as followed.

    1. It dosn't take the first input, it just ignores it, and wait for input number 2?

    and 2. If i type 20, which still is higher since ">" should take 19 and downwards, right?
    it terminates, without printing anything.

    So if someone could explain me those, and if i did something wrong, i would appreciate it.

    Regards

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    In which of these identical threads should we answer?

  3. #3
    Naxix is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by dlorde View Post
    In which of these identical threads should we answer?
    Sorry, i don't know what happend, i tried to see if i could delete one, but it dosn't seem to.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    It dosn't take the first input, it just ignores it,
    To verify that add some printlns to print out the value of Test immediately after you read it and
    immediately after the while() statement.

    If i type 20, .. it terminates
    What have you coded in your program to happen if Test == 20?
    Same answer as above. Add some printlns of the value of Test.

    BTW Java naming conventions say lowercase first letter for variables. Uppercase for classnames

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Naxix View Post
    Sorry, i don't know what happend, i tried to see if i could delete one, but it dosn't seem to.
    I removed your other thread for you; go on with the show in this thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Quote Originally Posted by Naxix View Post
    If i type 20, which still is higher since ">" should take 19 and downwards, right?
    Wrong. 20 is not greater than 20, it is equal.

    Another problem is if you enter a value less than 20 your second while loop will be infinite.

  7. #7
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    i modeifed the code ,i hope problem will be resolved
    Java Code:
    import java.util.Scanner;
    
    class Example {
    	public static void main(String args[]){
    		int Test;
    		Scanner Scan = new Scanner(System.in);
    		System.out.println("Guess the number: ");
    		Test = Scan.nextInt();
    		while(Test >= 20){
    			System.out.println("Too high, try again");
    			break;
    		}
    		while(Test < 20){
    			System.out.println("Spot on!");
    			break;
    		}
    	}
    }

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

    Default

    Java Code:
    while(Test < 20){
        System.out.println("Spot on!");
        break;
    }
    What is the point of that? A loop that only loops once is totally pointless. Since it is impossible to reach that point unless the value is 20 or less then just have the print statement and get rid of the loop.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You may want to consider using a do...while loop and doing tests in the loop. The do while loop ensures that stuff is done at least once, since you get the test variable before entering a loop you would favor from a do while.

    Java Code:
    do{
      get input
      if
        print message and continue
      end if
      if
        print message and break
      end if
    while(condition);
    This pseudo code does something, tests the input and continues the loop if a wrong answer is given, if a right answer is given, it ends the loop.

Similar Threads

  1. frame changes size even tho i told it not to :D
    By wildheart25c in forum AWT / Swing
    Replies: 0
    Last Post: 06-18-2011, 01:37 PM
  2. Code stops for no reason
    By Bagzli in forum New To Java
    Replies: 9
    Last Post: 03-03-2011, 04:56 AM
  3. Timer stops working :P
    By Addez in forum New To Java
    Replies: 13
    Last Post: 09-21-2010, 09:36 PM
  4. sound works then stops!
    By smileii in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-26-2009, 01:06 AM
  5. Stops when opening ObjectInputStream
    By Norberhuis in forum Networking
    Replies: 4
    Last Post: 01-09-2009, 05:19 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
  •