Search code examples
javaloopsjoptionpane

Loops and showConfirmDialog


I'm new to Java, and I need help. I created a game where the program generates a random number (1-100), and the user has to guess it. The game works. The problem I have is that when the user guesses the correct number, I have to ask the player, using showConfirmDialog, if they want to play again then use a loop to either play again or finish. Can anyone show me how to do so? Anything is greatly appreciated.

package guessinggame;

import javax.swing.JOptionPane;

public class Guessinggame {

public static void main(String[] args) {

    int guess;
    int numguess = 0;
    int usernum;

    int result = JOptionPane.showConfirmDialog(null, " Do you want to play? ", " Notice ", JOptionPane.YES_NO_OPTION);

    if (result == JOptionPane.YES_OPTION) {

        do {
            //game starts here
            guess = (int) (Math.random() * 100 + 1);
            usernum = Integer.parseInt(JOptionPane.showInputDialog("enter your guess"));
            numguess++;

            if (usernum > guess) {
                System.out.println(usernum + " is too high. Try again");
            } else if (usernum < guess) {
                System.out.println(usernum + " is too low. Try again");
            }
        } while (guess != usernum);

        int IQ = ((int) (Math.random() * 100 + 1)) + numguess;
        JOptionPane.showMessageDialog(null, " Correct, it took you " + numguess + " tries. Your IQ is " + IQ);

        //Use showConfirmDialog to if player wants to play again. 
        // If user chooses yes, play game again. If user chooses no end game
    } else if (result == JOptionPane.NO_OPTION) {
        JOptionPane.showMessageDialog(null, "Quiting? Bye!");
        System.exit(0);
    }

}

}

Solution

  • you can do it by wrapping everything in a while loop infinitely like this:

    package guessinggame;
    
    import javax.swing.JOptionPane;
    
    public class Guessinggame {
    
    public static void main(String[] args) {
    
        int guess;
        int numguess = 0;
        int usernum;
    
      while(true) { //Start of while loop
    
        int result = JOptionPane.showConfirmDialog(null, " Do you want to play? ", " Notice ", JOptionPane.YES_NO_OPTION);
    
        if (result == JOptionPane.YES_OPTION) {
    
            do {
                //game starts here
                guess = (int) (Math.random() * 100 + 1);
                usernum = Integer.parseInt(JOptionPane.showInputDialog("enter your guess"));
                numguess++;
    
                if (usernum > guess) {
                    System.out.println(usernum + " is too high. Try again");
                } else if (usernum < guess) {
                    System.out.println(usernum + " is too low. Try again");
                }
            } while (guess != usernum);
    
            int IQ = ((int) (Math.random() * 100 + 1)) + numguess;
            JOptionPane.showMessageDialog(null, " Correct, it took you " + numguess + " tries. Your IQ is " + IQ);
    
            //Use showConfirmDialog to if player wants to play again. 
            // If user chooses yes, play game again. If user chooses no end game
        } else if (result == JOptionPane.NO_OPTION) {
            JOptionPane.showMessageDialog(null, "Quiting? Bye!");
            System.exit(0);
        }
    
      } //End of While Loop
    
    }
    
    }