2016-11-15 2 views
0

showConfirmDialogウィンドウでユーザーが「いいえ」をクリックすると終了するwhileループ(Not a do ... while)を書き込もうとしています。しかし、プログラムを実行すると、[いいえ]をクリックしても、ループを繰り返して終了します。これは、NoNO変数をJOptionPane.NO_ANSWERに等しくすることができないためです.No_ANSWERをクリックしたときにNO_ANSWERになるはずですが、これは正しいですか?ユーザーが「いいえ」をクリックしたときにJOptionPaneのループを終了する方法は?

変数randとnumberに格納されている私のSecureRandomコードは、answers配列に乱数を生成していません。私はMath.random()を試してみましたが動作しませんでしたが、SecureRandomを使用してランダム配列インデックスを生成したいと考えています。

私のwhileループの最後の行は、userInputの値を空のStringに置き換えているわけではありません。入力した最初の質問でループを繰り返すだけです。その無限ループにとどまりたいと思っても、私が入力した新しい質問は記録されません。

ユーザーが「いいえ」をクリックすると、SecureRandomコードが生成されます(現在はインデックス19 Very Doubtfulのみを表示しています)、userInput変数は空のStringに変更されますか?または少なくともあなたのshowConfirmDialog方法は、常に常に​​あるoptionTypeを返し

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; 

    } 



} 

答えて

0

次の反復からの入力と現在の文字列に置き換えられます。ユーザーの入力を無視します。

あなたは常にrand.nextInt()と呼んでいます。これは常に同じ値を持つことを意味します。毎回新しい乱数が必要な場合は、rand.nextInt()への呼び出しをwhileループに移動します。

+0

rand.nextIntを()私のwhileループであり、それは数変数にあります –

1

WHILEループの前にプロンプ​​トが表示される必要はありません。 WHILEループプロンプトで十分ですが、そのWHILEループ内にランダム応答インデックス番号を生成する必要があります。そうでない場合、何回質問をしても同じ答えが返されます。私はそれが欲しいとは思わない。それはすべて自分自身でこれを行いますので、あなたは WHILEループ内でそれを再宣言するとき;(=「」ユーザ入力を)

あなたはUSERINPUT変数をクリアする必要はありません。変数はダイアログ入力ボックスに入力されたもので初期化され、何も供給されなければ何も( "")はの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); 
関連する問題