0
votes

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;
        }
    }
}

}
2
scan.nextLine() inside the else - theblindprophet

2 Answers

0
votes
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;
    }
}

You have not nested it wrong, but you don't have a condition that aborts the while loop.

You will set answer to 0, then go back to the beginning of the while loop & find that the guess is now too high because the answer was 0.

I think you wanted to do guess = 0;. Or you could put a break; there too.

0
votes

As you have

 while (guess != 0){

change your code to break out the loop

else {
            System.out.println("Your guess was correct!");
            attempts++;
            System.out.println("Number of guesses: " + attempts);
            answer = 0;
            break; // add this or change above line to guess = 0;
 }