JOptionPane cancel button will not be canceled from the window?

So, I am making a simple guessing game, and the program will not exit the loop when the user presses the cancel button. Here 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)); } 
+6
source share
1 answer

he should be

 while(playAgain == true){ 

or

 while(playAgain){ 

do not set [ = ] true for playagain use comparison [ == ]. what you do is set to true playagain and then checks to see if it is true.so is always true

+10
source

All Articles