Search code examples
javaloopsinfinite

Infinite pre-test while loop (java)


I am trying to create a guessing game program. The user enters a number and is told if the number is too high or low, then is told to guess again. I made an infinite loop and i cannot figure out how to change it. I realize that if the guess is wrong, then the program will keep checking the wrong value and printing a "wrong number" message.

package guessinggame;
import java.util.Scanner;
/**
 *
 * @author
 */
public class GuessingGame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        Scanner input = new Scanner (System.in);

        int guesses;    //number of users guesses

        int housePick;  //number the user must guess

        int guess;      //users guess

        housePick = (int)((Math.random() * 100) +1 );  
        //sets housePick to random number from 1 to 100

        System.out.println("I'm thinking of a number between 1 and 100") ;
        //print "Im thinking of a nubmer between 1 and 100"

        System.out.println("Can you guess what it is?");
        //print "can you guess what it is"

        System.out.println
                ("Enter a number from 1 to 100 (including 1 and 100)");
        //prompt user to enter number

        guess = input.nextInt();
        //save entered number as guess

        while (guess != housePick)  //while guess doesnt = housePick...
        {
            if (guess > housePick)  //and if guess > housePick...
            {
                if ((guess - 10) <= housePick )  
                    //and if guess is 10 numbers away from housePick...

                {
                    System.out.println("Close, but too high. Try again.");
                    //print "close but too high, try again"

                }
                else              //if guess is not close and guess>housePick...
                {
                    System.out.println ("Too high, try again.");  
                    //then print "Too high, Try again"
                }                         
            }
            else  //If guess<housePick
            {
            if ((guess + 10) >= housePick)  //AND if guess is close to housePick
            {
                System.out.println ("close, but too low.") ; 
                //then print "close, but too low"

            }
            else//If guess isnt close to housePick and is less than housePick...
            {
                System.out.println ("Too low.");//then print "too low"
            }

            }

        }



        System.out.println ("You win!  It took you "  + "guesses.");
        //If guess = housePick print "Yout win! It took you (# of guesses)"




    }
}

Solution

  • You never get a user selection and change the guess variable's value from within the while loop. If guess is never changed, the loop will never end since this never changes: while (guess != housePick) and the condition remains false.

    Solution:
    Do the obvious: use your input Scanner variable to get user input from inside the while loop, and use it to re-set guess to a new value.