簡體   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