1

I have written the code below. I have run the program and it allows the user to guess the correct number and return the message successfully. However, I couldn't get it to regenerate a new random number? I also couldn't include an option to ask whether the user wants to quit or not. Please help. Thank you.

import java.util.*;

class CompterAge {
  public static void main (String[] args) {
    Scanner sc = new Scanner(System.in);

    boolean correctGuess = false;
    
    Random r = new Random();
    int randNum = r.nextInt(100-1+1) + 1;
      
      while (!correctGuess) {
        System.out.println("Guess the computer's age: ");
        int guess = sc.nextInt();
        
        if ((guess > 0) && (guess <= 100)) {
              if (guess > randNum) {
                System.out.println("Your guess is bigger than the number. You should go lower.");
                correctGuess = false;
              } else if (guess < randNum) {
                System.out.println("Your guess is smaller than the number. You should go higher.");
                correctGuess = false;
              } else {
                System.out.println("Congratulations. You got the number! The number is " + randNum);
                System.out.println("Do you wish to continue the game? Yes/No");
                String input = sc.next();
                if (input == "Yes") {
                  correctGuess = false;
                } else {
                  break;
                }
              }
        } else {
        System.out.println("Please enter a number between 1 to 100.");
        correctGuess = false;
        }
      } 
      
}
}
  • Relevant: [How do I compare Strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) The issue is here `if (input == "Yes") {` - should be `if (input.equals("Yes")) {`. – maloomeister Jul 02 '21 at 08:53

2 Answers2

1

This code works, I have added another flag variable and corrected some logical errors. See the comments.

boolean correctGuess = false;
boolean endGame = false;

Random r = new Random();

while (!endGame){  
    int randNum = r.nextInt(101); //Why did you have (100-1+1) + 1 ?? Simple (101) was enough
    correctGuess = false;
    while (!correctGuess) {
        System.out.println("Guess the computer's age: ");
        int guess = sc.nextInt();
        
        if ((guess > 0) && (guess <= 100)) {
            if (guess > randNum) {
                System.out.println("Your guess is bigger than the number. You should go lower.");
                correctGuess = false;
            } else if (guess < randNum) {
                System.out.println("Your guess is smaller than the number. You should go higher.");
                correctGuess = false;
            } else {
                correctGuess = true; //Will exit the Inner Loop
                System.out.println("Congratulations. You got the number! The number is " + randNum);
                System.out.println("Do you wish to continue the game? Yes/No");
                String input = sc.next().toLowerCase();
                if (input.equals("yes")) { //You can not use == for String Comparisons
                endGame = false;
                } else {
                endGame = true;
                }
            }
        } else {
        System.out.println("Please enter a number between 1 to 100.");
        correctGuess = false;
        }
    } 
}
0

Your random number generation is done outside of your while loop so when they input that they want to continue the game it should then generate a new number:

System.out.println("Congratulations. You got the number! The number is " + randNum);
System.out.println("Do you wish to continue the game? Yes/No");
String input = sc.next();
if (input.equals("Yes")) {
     randNum = r.nextInt(100-1+1) + 1;
     correctGuess = false;
} else {
     break;
}
DevWithZachary
  • 3,545
  • 11
  • 49
  • 101
  • Hi. I have added that code to my program but after I entered "Yes", the program didn't prompt me to guess the computer age (which is to go back to the start of the while loop.) – nicetomeetyou98 Jul 02 '21 at 08:44
  • 1
    [How do I compare Strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – maloomeister Jul 02 '21 at 08:53
  • 1
    Didnt fully review your code, my bad. I have updated my example, as @maloomeister pointed out you should be using the equals method not == – DevWithZachary Jul 02 '21 at 08:57