I am writing a simple guessing game program where the user will input a number to try and guess a randomly generated number.
If they get the number right I want to give them the option to play again.
Here is my code:
public class GuessingGame {
private Random num = new Random();
private int answer = num.nextInt(10);
private int guess;
private String playAgain;
public void inputGuess(){
System.out.println("Enter a number between 1 and 10 as your first guess: ");
Scanner input = new Scanner(System.in);
guess = input.nextInt();
do{
if (guess < 1 || guess > 10){
System.out.println("That is not a valid entry. Please try again: ");
guess = input.nextInt();
}else if (guess > answer){
System.out.println("Too high, Try Again: ");
guess = input.nextInt();
}else if (guess < answer){
System.out.println("Too low, Try Again: ");
guess = input.nextInt();
}
}while (guess != answer);
System.out.println("Congratulations, You guessed the number!");
System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");
playAgain = input.nextLine();
if(playAgain == "Y" || playAgain == "y"){
System.out.println("Enter a number between 1 and 10 as your first guess: ");
guess = input.nextInt();
}
}
}
The game plays through but when the user is prompted to play again nothing happens?
Any suggestions?
Try something like this:
You need your code for checking if they want to play again inside the loop. This way you wait until they have guessed the number correctly then ask if they want to play again. If they do you restart the process if they don't you exit the loop.
Some of the solution I see above aren't correct. The random number, you need to add 1 to get between 1 and 10, also you need to compare with equals. I use case insensitive here.
The following code works as you need it.
Do the following and your code will work :
input.nextInt();
withInteger.parseInt(input.nextLine());
(playAgain == "Y" || playAgain == "y")
with(playAgain.equalsIgnoreCase("Y"))
answer
insideinputGuess()
in starting instead.if(playAgain.equalIgnoreCase("Y"))
withinputGuess();
When you enter integer value through console it also contain a
\n
(next line) in it. But when you usenextInt()
, it doesn't read this\n
, but then when you tried to get next line withinput.nextLine()
, it looks for\n
(next line) which is already there from integer entry and having nothing after that. Code look for "Y" or "y" and breaks because it doesn't found any of them.That is why
Integer.parseInt(input.nextLine());
works hereHere is the code:
You just need to introduce the winning/losing logic inside the while, and the condition will be the ending/continue flag.
Another thing is always remember when comparing strings to use the equals method, since the == will compare the object reference and not the String value, in some cases == will return true for equal string since how JVM stores the Strings, but to be sure always use equals.
This code can serve your purpose...
one issue :
Do not compare the content of two strings by
==
which just you should useequals()
Imagine the right answer is one in this case
Follow this as your blue print sample
output: