[英]Multiple conditions while loop for scanner validation
Hi guys trying to validate an integer range for user input in java.大家好,试图验证 java 中用户输入的 integer 范围。 I am new to programming and java.
我是编程和 java 的新手。 The part i am having difficulty with is the multiple & near the end.
我遇到困难的部分是倍数和接近尾声。 I am not sure how to approach it so have guessed.
我不确定如何处理它,所以已经猜到了。 I am unable to find a proper solution elsewhere;
我无法在其他地方找到合适的解决方案;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// Create a Scanner
Scanner sc = new Scanner(System.in);
int number;
do {
System.out.println("Please enter window width");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
number = sc.nextInt();
} while ((width > 0.5 & <2.5) && (height >0.5 & <3.5));
System.out.println("Window is:" + height + "m high " + width + "m wide.");
}
It seems that your do while loop syntaxis is not correct, try with this code:您的 do while 循环语法似乎不正确,请尝试以下代码:
do {
System.out.println("Please enter window width");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
number = sc.nextInt();
}
while((width > 0.5 && width < 2.5) && (height > 0.5 && height < 3.5));
Parse the input using Double#parseDouble
and in case it does not succeed (ie if an exception is thrown), loopback to ask the user to try again.使用
Double#parseDouble
解析输入,如果它不成功(即如果抛出异常),则回送以要求用户重试。 After validating the input (whether it is a number), validate the range as per your requirement.验证输入(是否为数字)后,根据您的要求验证范围。
Do it as follows:执行以下操作:
import java.util.Scanner;
class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// Variables for width and height
double width, height;
// Input width
do {
width = getDimennsion("Please enter window width between 0.5 & 2.5: ");
} while (width < 0.5 || width > 2.5);// Loop back in case of invalid dimension
// Input height
do {
height = getDimennsion("Please enter window height between 0.5 & 3.5: ");
} while (height < 0.5 || height > 3.5);// Loop back in case of invalid dimension
System.out.println("Width: " + width + ", Height: " + height);
// ...Rest of the processing
}
static double getDimennsion(String msg) {
boolean valid;
double num = 0;
do {
valid = true;
System.out.print(msg);
try {
// Get input
num = Double.parseDouble(sc.nextLine());
} catch (IllegalArgumentException e) {
System.out.println("Invalid input. Please try again");
valid = false;
}
} while (!valid);// Loop back in case of invalid input
return num;
}
}
A sample run:示例运行:
Please enter window width between 0.5 & 2.5: a
Invalid input. Please try again
Please enter window width between 0.5 & 2.5: 10
Please enter window width between 0.5 & 2.5: 2
Please enter window height between 0.5 & 3.5: b
Invalid input. Please try again
Please enter window height between 0.5 & 3.5: 15
Please enter window height between 0.5 & 3.5: 3
Width: 2.0, Height: 3.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.