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));
}
If it's really safe to just kill all JOptionPanes you can do something like this: public static void main(String[] args) { new Thread() { public void run() { for (int i = 0; i < 3; i++) { try { Thread. sleep(2000); } catch (InterruptedException e) { } JOptionPane. getRootFrame(). dispose(); } } }.
Check the code below: JOptionPane pane = new JOptionPane("message"); JDialog dialog = pane. createDialog(null, "Title"); dialog. setDefaultCloseOperation(WindowConstants.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With