我的Do While循环中缺少或添加了一些内容,我该如何修复它


String message = "Hello " + name + ", Do you know what day it is today?";
answer = JOptionPane.showConfirmDialog(frame, message);
JOptionPane.showMessageDialog(null, "Come on think a little harder", "Are you new here?",
JOptionPane.INFORMATION_MESSAGE);
} while (answer == JOptionPane.NO_OPTION);
if (answer == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Great!", "Yay", JOptionPane.INFORMATION_MESSAGE);
}
}
}

当我按"否"时,它会说";来吧,想得更难一点&";然后循环回"你知道今天是星期几吗",但当我按"是"时,它仍然会说"来吧,想得更难一点";然后只说";伟大的

当我按下"是"时,我想让它说的只是"很棒"的

我想我可能把一些不应该/不应该在while循环中的东西放错地方了,我真的不确定。。

在确定为无消息之前,您显示的是无消息:

while (true) {
String message = "Hello " + name + ", Do you know what day it is today?";
int answer = JOptionPane.showConfirmDialog(null, message);
if (answer == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Come on think a little harder", "Are you new here?", JOptionPane.INFORMATION_MESSAGE);
} else if (answer == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Great!", "Yay", JOptionPane.INFORMATION_MESSAGE);
break;
} else {
break;
}
}

您只需要添加一个if条件,否则yesno选项都将执行Come on think a little harder

do {
String message = "Hello " + name + ", Do you know what day it is today?";
answer = JOptionPane.showConfirmDialog(frame, message);
if (answer == JOptionPane.NO_OPTION) { // Add if condition
JOptionPane.showMessageDialog(null, "Come on think a little harder", "Are you new here?",
JOptionPane.INFORMATION_MESSAGE);
}
} while (answer == JOptionPane.NO_OPTION);
if (answer == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Great!", "Yay", JOptionPane.INFORMATION_MESSAGE);
}

最新更新