I am attempting to make a High Low number guessing game that reports to the user if their guess is too high, too low or accurate. Everything is working as expected with the exception of a successful guess (of all things). Upon entry of the correct number, the loop prints the "Your guess was correct" statement along with the number of tries.This is what I want. However, instead of asking if the user would like to replay next, it is returning the "too high" response. Have I nested this incorrectly?
import java.util.Scanner;
import java.util.Random;
public class hilo2 {
public static void main(String[] args) {
int guess = 0;
int attempts;
String quit;
boolean replay = true;
Scanner scan = new Scanner(System.in);
Random generator = new Random();
while (replay) {
attempts = 0;
int answer = generator.nextInt(101) + 1;
System.out.println(answer);//for testing
System.out.println("Guess a number between 1 and 100. Enter your guess (0 to quit): ");
guess = scan.nextInt();
scan.nextLine();
while (guess != 0){
if (guess < answer) {
System.out.println("You guess was too low. Guess again (0 to quit): ");
guess = scan.nextInt();
scan.nextLine();
attempts++;
}
else if (guess > answer) {
System.out.println("Your guess was too high. Guess again (0 to quit): ");
guess = scan.nextInt();
scan.nextLine();
attempts++;
}
else {
System.out.println("Your guess was correct!");
attempts++;
System.out.println("Number of guesses: " + attempts);
answer = 0;
}
}
System.out.println("Would you like to play again? (y/n): ");
String input = scan.nextLine();
if (input.equalsIgnoreCase("n")) {
replay = false;
}
}
}
}
scan.nextLine()inside theelse- theblindprophet