0

Try to make my code so that I can ask the user if they want to play again, however I'm confused how I'm supposed to go about this.

Scanner in = new Scanner(System.in);
int randomNum = 1 + (int)(Math.random() * 100);
System.out.println(randomNum);

Boolean playAgain = true;

while (playAgain) {
    System.out.print("I'm thinking of a number between 1 and 100.\nWhat is it?\nGuess: ");      
    int guessNum = in.nextInt();

    while ((guessNum > randomNum) || (guessNum < randomNum)) {
        if (guessNum > randomNum) {
            System.out.print("Too High.\nGuess: ");
            guessNum = in.nextInt();
        } else if (guessNum < randomNum) {
            System.out.print("Too Low.\nGuess: ");
            guessNum = in.nextInt();
        }           
    } 

    System.out.println("You got it!\nPlay again? (Y/N) ");

    String answer = in.next();

    if (answer == "y") {
        playAgain = true;
    } else {
        playAgain = false;
        System.out.println("Thanks for playing!");
    }
}
2
  • Use do...while() Commented Nov 23, 2016 at 4:00
  • Do while works but now I can't get it to NOT loop Commented Nov 23, 2016 at 4:08

3 Answers 3

1

You have used == equals Relational Operator which usually used to check string reference not the value actually. So, even if answer contains y then your conditional expression never returns true as a Result.

For Reference : Visit Here For More Info ( '==' vs 'equals()' method)

String answer = in.next();

//problem with this line becuase == operator use to check reference instead of value
if (answer == "y") {  
    playAgain = true;
} else {
    playAgain = false;
    System.out.println("Thanks for playing!");
}

So change your code as like below

String answer = in.next();

    if (answer.startsWith("y") || answer.startsWith("Y")) {  // Now this willl work fine to your code
        playAgain = true;
    } else {
        playAgain = false;
        System.out.println("Thanks for playing!");
    }
Sign up to request clarification or add additional context in comments.

Comments

0

It May help You

do {
            System.out.print("I'm thinking of a number between 1 and 100.\nWhat is it?\nGuess: ");
            int randomNum = 1 + (int) (Math.random() * 100);
            System.out.println(randomNum);

            int guessNum = in.nextInt();

            do {

                if (guessNum > randomNum) {
                    System.out.print("Too High.\nGuess: ");
                    guessNum = in.nextInt();
                } else if (guessNum < randomNum) {
                    System.out.print("Too Low.\nGuess: ");
                    guessNum = in.nextInt();
                }
            }while ((guessNum > randomNum) || (guessNum < randomNum));

                System.out.println("You got it!\nPlay again? (Y/N) ");

                answer = in.next();
        }while(answer.equals("y"));

Comments

0

If you want to use playAgain var, then use

answer.equals("y")

to compare two strings instead of ==.

otherwise have your full code inside while block or do while. And if user wants to play, then do continue, otherwise do break;

   while(true)
     {
       // your code
       String answer = in.next();

    if (answer == "y") {
        continue;
    } else {
        System.out.println("Thanks for playing!");
        break; 
    }
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.