【发布时间】:2013-03-19 18:23:00
【问题描述】:
我正在尝试一个简单的 do while 循环,假设在输入小于 1 且大于 1000 时运行。它应该要求用户输入正确的数字,否则在循环中。它现在似乎正在做的是再重复一次循环,要求正确的输入,然后显示结束消息。如果满足条件,不确定为什么会重复它
String name = JOptionPane.showInputDialog(null,
"Please enter students lastname");
int input = Integer.parseInt(JOptionPane.showInputDialog(null,
"Please enter students ID"));
do {
JOptionPane.showMessageDialog(null,
"Please enter a student ID within the correct parameters");
input = Integer.parseInt(JOptionPane.showInputDialog(null,
"Please enter students ID"));
} while (input < 1 && input > 1000);
// Output dialog with user input
JOptionPane.showMessageDialog(null, "StudentID: " + input
+ "\nStudent Last: " + name);
【问题讨论】:
-
你能提供一个小于 1 且大于 1000 的数字的例子吗?
-
你明白 do-while 执行 before 检查条件,对吗?所以它会一直运行到 input==1002
-
(input < 1 && input > 1000)永远不会是真的! -
条件应该是
(input < 1 || input > 1000),接受1到1000之间的数字,对吧?