繁体   English   中英

用户单击否时如何退出JOptionPane的while循环?

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

我正在尝试编写一个while循环(不是do ... while),一旦用户在showConfirmDialog窗口上单击“否”,该循环就会退出。 但是,当我运行该程序时,即使单击“否”或尝试退出该程序,它仍会循环遍历该循环。 我怀疑这是因为我不能使yesNO变量等于JOptionPane.NO_ANSWER,即使当我单击No ...对时它应该变成NO_ANSWER吧?

我存储在变量rand和number中的SecureRandom代码未在Answers数组中生成随机数。 我已经尝试过Math.random(),但是它没有用,但是我想使用SecureRandom生成随机数组索引。

我的while循环的最后一行没有用空的String代替userInput值。它只是在循环中输入了第一个问题。 因此,即使我希望停留在无限循环中,它也不会记录我输入的新问题...

我如何才能使它在用户单击否时退出循环,SecureRandom代码每次都会生成不同的答案索引(当前仅显示索引19 Very Doubtful),并且userInput变量被更改为空白String? 或至少用下一次迭代的输入替换当前字符串

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;

    }



}

WHILE循环之前,您不需要提示。 WHILE循环提示就足够了,但是您需要在该WHILE循环内生成随机答案索引号,否则无论问多少个问题,您都将始终提供相同的答案。 我不想要你

您不需要清除userInput变量(userInput =“” ;;),因为当您在WHILE循环中重新声明它时,它会自行完成所有操作。 变量将使用在对话框输入框中输入的任何内容进行初始化,如果未提供任何内容,则userInput将保留任何内容(“”)。

showMessageDialog()方法的调用中所有/ n的含义是什么。 哎呀...那些应该是\\ n的;)

所有人应该看起来像这样:

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);

您的showConfirmDialog方法始终返回optionType ,始终为JOptionPane.YES_NO_OPTION 它忽略用户输入。

您只调用一次rand.nextInt() ,这意味着您将始终拥有相同的值。 如果每次都需要一个新的随机数,请rand.nextInt()的调用移至while循环中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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