我尝试获取用户输入以填充具有< = 1且< = 24的数字的变量。目前它只发送到else语句并打印这两行并等待另一个输入。即使输入满足三个条件。
EG。我进入1.这应该满足所有三个条件?
程序应该为用户提供与输入问题相对应的错误。
如果未输入整数,则仅要求输入数字。如果该数字是<1或> 24,则应该要求仅在这两个点之间输入数字。
while (length1 == Integer.MIN_VALUE) {
System.out.print("Enter the Length of wall 1: ");
if (scan.hasNextInt() && 1<=length1 && length1<=24) {
length1 = scan.nextInt();
}
else if (length1<1 && 24<length1) {
System.out.println("Error Error");
System.out.println("Please only enter numbers between 1 and 24.");
scan.next();
} else {
System.out.println("Error Error");
System.out.println("Please only enter numbers.");
scan.next();
}
}
答案 0 :(得分:1)
除了一个错误的&&
之外,首先应该读取nextInt。
while (true) {
System.out.print("Enter the Length of wall 1: ");
if (scan.hasNextInt()) {
length1 = scan.nextInt();
if (1 <= length1 && length1 <= 24) {
break:
}
System.out.println("Error Error");
System.out.println("Please only enter numbers between 1 and 24.");
} else {
System.out.println("Error Error");
System.out.println("Please only enter numbers.");
scan.next();
}
}
答案 1 :(得分:0)
使用以下代码:
while (length1 == Integer.MIN_VALUE) {
System.out.print("Enter the Length of wall 1: ");
if (scan.hasNextInt() && 1<=length1 && length1<=24) {
length1 = scan.nextInt();
}
else if (length1<1 || 24<length1) {
System.out.println("Error Error");
System.out.println("Please only enter numbers between 1 and 24.");
scan.next();
} else {
System.out.println("Error Error");
System.out.println("Please only enter numbers.");
scan.next();
}
}