guccimanilla
guccimanilla

Reputation: 33

How to make code loop with boolean specified at the end of code?

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!");
    }
}

Upvotes: 0

Views: 45

Answers (3)

Bhagwati Malav
Bhagwati Malav

Reputation: 3549

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

Upvotes: 0

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6856

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!");
    }

Upvotes: 1

Srinivasu
Srinivasu

Reputation: 1235

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"));

Upvotes: 0

Related Questions