简体   繁体   中英

How to exit while loop for JOptionPane when user clicks no?

I am trying to write a while loop (Not a do...while) that exits once the user clicks "No" on the showConfirmDialog window. However, when I run the program, it keeps iterating through the loop even if I click No or try to exit out. I suspect this is because I cannot get the yesNO variable to equal JOptionPane.NO_ANSWER, even though it's supposed to turn into NO_ANSWER when I click on No...right?

My SecureRandom code stored in the variables rand and number aren't generating random numbers in the answers array. I've tried Math.random() and it didn't work but I want to use SecureRandom to generate the random array index.

The last line of my while loop isn't replacing userInput value with an empty String.. it just iterates over the loop with the first question typed. So even if I WANTED to stay in that infinite loop, it doesn't even record the new question I type in...

How can I make it so the loop exits when the user clicks no, the SecureRandom code generates a different answer index every time (currently it only shows index 19 Very Doubtful), and the userInput variable gets changed to a blank String? or at least replaces the current string with the input from the next iteration

import javax.swing.*;
import java.math.*;


public class MagicEightBall {


    public static void main(String[] args) {

        Random rand = new SecureRandom();
        int number = rand.nextInt(20);

        //array for answers
        String [] answers = {"It is certain", "It is decidedly so", "Without a doubt",
                "Yes - definitely", "You may rely on it", "As I see it, yes",
                "Most likely", "Outlook good", "Signs point to yes",
               "Yes", "Reply hazy, try again", "Ask again later",
               "Better not tell you now", "Cannot predict now", "Concentrate and ask again",
               "Don't count on it", "My reply is no", "My sources say no",
               "Outlook not so good", "Very doubtful"};

         ImageIcon image = new ImageIcon("8ball_small.jpg");

         // prompt user for question
         String userInput = JOptionPane.showInputDialog(null, "What is your question?", "Welcome to MAGIC 8-BALL", JOptionPane.QUESTION_MESSAGE);

         //return answer (Always returns "Very Doubtful)
         showMessageDialog(userInput + "\n\n\n\n" + answers[number],"MAGIC 8-BALL SAYS: ",0,image);


         int yesNo = showConfirmDialog("","ASK MAGIC 8-BALL AGAIN", JOptionPane.YES_NO_OPTION, 0, image);

         // ask user to stop or continue asking questions (begins loop no
         //matter what input)

         while (yesNo == JOptionPane.YES_OPTION) {

           userInput = JOptionPane.showInputDialog(null, "What is your question?", "Welcome to MAGIC 8-BALL", JOptionPane.QUESTION_MESSAGE);
           showMessageDialog(userInput + "\n\n\n\n" + answers[number],"MAGIC 8-BALL SAYS: ",0,image);
           yesNo = showConfirmDialog("","ASK MAGIC 8-BALL AGAIN", JOptionPane.YES_NO_OPTION, 0, image);
           userInput = ""; // doesn't reset userInput to an empty string for
                           // next iteration
        }

           showMessageDialog("/n/n/n/n Programmed By Jason Silva","GOODBYE ",0,image);


}


//teacher wants us to write methods for the dialog boxes

public static void showMessageDialog(String message, String title, int messageType, ImageIcon image) {

        JOptionPane.showMessageDialog (null, message, title, messageType, image);


    }


public static int showConfirmDialog(String message, String title,
                                    int optionType, int messageType, ImageIcon icon) {

        JOptionPane.showConfirmDialog(null, message, title, optionType, messageType, icon);
        return optionType;

    }



}

You don't need your prompts before the WHILE loop. The WHILE loop prompts will suffice however you will need to generate your random answer index number within that WHILE loop otherwise you will always provide the same answer no matter how many questions are asked. I don't think yo want that.

You don't need to clear the userInput variable (userInput = "";) since it does this all by itself when you re-declare it within the WHILE loop. The variable is initialized with whatever is entered within the dialog input box and if nothing is supplied then nothing ("") is what userInput will hold.

What is with all the /n 's within the call to the showMessageDialog() method. Whoops...those should be \\n 's ;)

All should look something like this:

Random rand = new SecureRandom();

//array for answers
String [] answers = {"It is certain", "It is decidedly so", "Without a doubt",
        "Yes - definitely", "You may rely on it", "As I see it, yes",
        "Most likely", "Outlook good", "Signs point to yes",
        "Yes", "Reply hazy, try again", "Ask again later",
        "Better not tell you now", "Cannot predict now", "Concentrate and ask again",
        "Don't count on it", "My reply is no", "My sources say no",
        "Outlook not so good", "Very doubtful"};

ImageIcon image = new ImageIcon("8ball_small.jpg");

int yesNo = -1;
while (yesNo != JOptionPane.NO_OPTION) {
    int number = rand.nextInt(20);
    String userInput = JOptionPane.showInputDialog(null, "What is your question?", 
                       "Welcome to MAGIC 8-BALL", JOptionPane.QUESTION_MESSAGE);

    showMessageDialog(userInput + "\n\n\n\n" + answers[number],"MAGIC 8-BALL SAYS: ",0,image);

    yesNo = showConfirmDialog("","ASK MAGIC 8-BALL AGAIN", JOptionPane.YES_NO_OPTION, 0, image);
}

showMessageDialog("\n\n\n\n Programmed By Jason Silva","GOODBYE ",0,image);

Your showConfirmDialog method always returns optionType which is always JOptionPane.YES_NO_OPTION . It ignores user input.

You're calling rand.nextInt() exactly once, which means you'll always have the same value. Move the call to rand.nextInt() into your while loop if you want a fresh random number every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM