Reputation: 97
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)"
}
}
Upvotes: 1
Views: 680
Reputation: 4766
You are correct up to some level but when the things go wrong you have to get the input from the user before the loop ends. So you have to get the input from the player just before the while loop ends.I have done the correction and The updated code (only the while loop part) as follows
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"
}
}
/// this is the correction
System.out.println
("Enter a number from 1 to 100 again (including 1 and 100)");
//prompt user to enter number
guess = input.nextInt();
}
Upvotes: 1
Reputation: 1384
As mentioned by others, the reason you are having issues stems from the fact that you are not looping input, only checking. Putting your input.nextInt()
into the while loop will solve this issue.
Also, as a point of procedure, this is the appropriate location to use a do/while loop rather than a normal while (as you always want to run the input at least once).
Upvotes: 0
Reputation: 10497
Add below line just before the while
loop ends so that it will ask every time when the guess is wrong and successfully exit from the loop when guess is right.
while (guess != housePick) //while guess doesnt = housePick...
{
if (guess > housePick)
{
\\Your code remains as it is.
...
...
...
...
} //if-else loop ends here.
guess = input.nextInt();
}//while ends here.
Upvotes: 0
Reputation: 285405
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.
Upvotes: 1