我正在尝试编写一个while循环(不是do ... while),一旦用户在showConfirmDialog窗口中单击“No”就会退出。但是,当我运行程序时,即使我单击否或尝试退出,它仍会循环遍历循环。我怀疑这是因为我无法让yesNO变量等于JOptionPane.NO_ANSWER,即使我点击No ...它应该变成NO_ANSWER对吧?
存储在变量rand和number中的我的SecureRandom代码不会在answers数组中生成随机数。我已经尝试过Math.random()并且它不起作用,但我想使用SecureRandom来生成随机数组索引。
我的while循环的最后一行不是用空字符串替换userInput值。它只是在键入第一个问题的循环中迭代。所以,即使我想要留在那个无限循环中,它也不会记录我输入的新问题......
如何在用户单击“否”时退出循环,SecureRandom代码每次生成不同的答案索引(目前它只显示索引19非常可疑),并且userInput变量变为空字符串?或者至少用下一次迭代的输入替换当前字符串
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 :(得分:1)
在 WHILE 循环之前,您不需要提示。 WHILE 循环提示就足够了,但是你需要在WHILE循环中生成随机答案索引号,否则无论问多少问题,你都会提供相同的答案。我不认为你想要那个。
您无需清除 userInput 变量(userInput =“”;),因为当您在 WHILE 中重新声明它时,它会自行完成此操作环。使用在对话框输入框中输入的内容初始化变量,如果没有提供任何内容,则 userInput 将保留任何内容(“”)。
showMessageDialog()方法调用中的所有 / n 是什么?哎呀......那些应该是 \ n 的;)
所有应该看起来像这样:
<iframe width="560" height="315"
src="https://www.youtube.com/embed/8gESFEtIx1A" frameborder="0"
allowfullscreen>
</iframe>
答案 1 :(得分:0)
您的showConfirmDialog
方法始终返回optionType
,始终为JOptionPane.YES_NO_OPTION
。它忽略了用户输入。
您正在调用rand.nextInt()
一次,这意味着您将始终拥有相同的值。如果您每次都想要一个新的随机数,请将呼叫转移到rand.nextInt()
。