TheSuds13
TheSuds13

Reputation: 347

JOptionPane cancel button won't cancel out of the window?

So, I'm making a simple guessing game, and the program won't exit out of the loop when the user presses the cancel button. Here's the loop

while(playAgain = true){
        int n = JOptionPane.showConfirmDialog(null, fields, "Number guessing game", JOptionPane.CANCEL_OPTION);

        if(n == JOptionPane.CANCEL_OPTION){
            playAgain = false;
        }

        int randomNumber = randomNumber();

        String guess = input.getText();
        compare(randomNumber, Integer.parseInt(guess));

    }

Upvotes: 6

Views: 278

Answers (1)

Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9872

it should be

while(playAgain == true){

or

while(playAgain){

don't assign [=] true to playagain use comparison [==] . what you do is assign true to playagain and then check is it true.so it's always true

Upvotes: 11

Related Questions