This is the code I have so far right now.

import java.util.Scanner;
import java.util.Random;

public class GuessANumber {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter a random seed: ");
        int seed = scanner.nextInt();
        Random rnd = new Random (seed);
        
        int rightAnswer = rnd.nextInt(200)+1;
        
        int numAttempts = 0;
        
        System.out.print("Enter a guess between 1 and 200: ");
        int guess = scanner.nextInt();
        numAttempts ++;
        
        while (guess != rightAnswer) {
           if (guess < 1 || guess > 200) {
              System.out.println("Your guess is out of range. Pick a number between 1 and 200.");
           }
           if (guess > rightAnswer) {
              System.out.println("Your guess was too high - try again.");
           }
           else if (guess < rightAnswer) {
              System.out.println("Your guess was too low - try again.");
           }
           System.out.println();
           System.out.print("Enter a guess between 1 and 200: ");
           guess = scanner.nextInt();
           numAttempts ++;
        }
        
        if (guess == rightAnswer) {
           System.out.println("Congratulations!  Your guess was correct!");
           System.out.println();
           System.out.println("I had chosen " + rightAnswer + " as the target number.");
           System.out.println("You guessed it in " + numAttempts + " tries.");
        }
        
        if (numAttempts == 1) {
           System.out.println("That was impossible!");
        }
        else if (numAttempts == 2 || numAttempts == 3) {
           System.out.println("You're pretty lucky!");
        }
        else if (numAttempts >= 4 && numAttempts <= 7) {
           System.out.println("Not bad, not bad...");
        }
        else if (numAttempts == 8) {
           System.out.println("That was not very impressive.");
        }
        else if (numAttempts == 9 || numAttempts == 10) {
           System.out.println("Are you having any fun at all?");
        }
        else if (numAttempts >= 11) {
           System.out.println("Maybe you should play something else.");
        }
        
        scanner.close();

    }
}

This is what the output should be :

Enter a random seed: Enter a guess between 1 and 200: Your guess is out of range.  Pick a number between 1 and 200.
Your guess was too low - try again.

Enter a guess between 1 and 200: Your guess was too low - try again.

Enter a guess between 1 and 200: Your guess is out of range.  Pick a number between 1 and 200.
Your guess was too high - try again.

However, the output my code provides on this test has an extra line of whitespace that shouldn't be there, like this :

Enter a random seed: Enter a guess between 1 and 200: Your guess is out of range. Pick a number between 1 and 200.
Your guess was too low - try again.

Enter a guess between 1 and 200: Your guess was too low - try again.

Enter a guess between 1 and 200: Your guess is out of range. Pick a number between 1 and 200.
Your guess was too high - try again.

E

Please let me know about any suggestions!

1

There are 1 best solutions below

0
Elliott Frisch On

if (guess == rightAnswer) { should be in your while loop body. Also, it needs a break. I would also suggest a do-while loop as you can then put the prompt in the loop. I also would not count invalid guesses as attempts. Putting that all together, it might look something like

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a random seed: ");
int seed = scanner.nextInt();
Random rnd = new Random(seed);

int rightAnswer = rnd.nextInt(200) + 1;
int numAttempts = 0;

do {
    System.out.println("Enter a guess between 1 and 200: ");
    int guess = scanner.nextInt();
    numAttempts++;
    if (guess < 1 || guess > 200) {
        System.out.println("Your guess is out of range. Pick a number between 1 and 200.");
        numAttempts--;
    } else if (guess > rightAnswer) {
        System.out.println("Your guess was too high - try again.");
    } else if (guess < rightAnswer) {
        System.out.println("Your guess was too low - try again.");
    } else if (guess == rightAnswer) {
        System.out.println("Congratulations!  Your guess was correct!");
        System.out.println();
        System.out.println("I had chosen " + rightAnswer + " as the target number.");
        System.out.println("You guessed it in " + numAttempts + " tries.");
        break;
    }
} while (true);

if (numAttempts == 1) {
    System.out.println("That was impossible!");
} else if (numAttempts <= 3) {
    System.out.println("You're pretty lucky!");
} else if (numAttempts <= 7) {
    System.out.println("Not bad, not bad...");
} else if (numAttempts == 8) {
    System.out.println("That was not very impressive.");
} else if (numAttempts <= 10) {
    System.out.println("Are you having any fun at all?");
} else {
    System.out.println("Maybe you should play something else.");
}