我提示用户输入一个整数,如果他们没有输入正确的整数(作为对选项的引用),那么我希望提示再次显示,直到他们输入为止。
到目前为止,这是我的代码:
int logIn = 0;
do {
logIn = Integer.parseInt(JOptionPane.showInputDialog(null,
"Please:"
+ "\n(Enter number value of option you would like to choose.)\n"
+ "\n1. Log In \n2. Register"));
} while (1 > logIn || logIn < 2);
int custIndex;
if (logIn == 1) {
custIndex = recommend.getCustomerIndex();
} else {
customers.printCustomers();
custIndex = customers.readCustomers().size();
}
int options = 0;
do {
options = Integer.parseInt(JOptionPane.showInputDialog(null,
"Would you like to:"
+ "\n(Enter number value of option you would like to choose.)\n"
+ "\n1. See your recommendations. \n2. See top rated books."
+ "\n3. See random books of the day. \n4. Exit."));
} while (1 > options || options < 4);
唯一的问题是我的应用程序无法正确通过登录。如果用户输入1,则会再次显示提示;如果用户输入任何大于 2 的数字,无论如何都会将他们带到第二个选项。
如有任何帮助,我们将不胜感激。
最佳答案
您的 while
条件读取
while (1 > logIn || logIn < 2)
这意味着输入必须小于 1 或小于 2 才能继续循环。你想要的是
while (logIn < 1 || logIn > 2)
或者也许更清晰
while (logIn != 1 && logIn != 2)
关于java - Java do while 循环的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9203404/