多输入验证合并 Java
Multiple Input Validation Consolidation Java
当我收到用户的输入时,我想确保它是:
- 一个数字
- 大于最小值
我写了下面的代码来实现这个,但它看起来比它必须的更复杂。有没有办法合并问题输入的是一个数字吗,这个数字是否小于十,或者任何类似的两部分验证?
// function prompts user for a double greater than number passed in
// continues to prompt user until they input a number greater than
// the minimum number
public static double getInput(double minimumInput) {
Scanner scan = new Scanner(System.in);
double userInput;
System.out.print("Enter a number greater than " + minimumInput + ": ");
while (!scan.hasNextDouble()){
String garbage = scan.next();
System.out.println("\nInvalid input.\n");
System.out.print("Enter a number greater than " + minimumInput + ": ");
} // end while
userInput = scan.nextDouble();
while (userInput <= minimumInput) {
System.out.println("\nInvalid input.\n");
userInput = getInput(minimumInput);
}
return userInput;
} // end getInput
简单回答:没有。
你看,用户输入可以是任何东西。如果您不使用 "nextDouble()" 方法,您的代码甚至必须将字符串转换为数字。但是在java中没有办法说:这个东西是double,和它一定比其他一些值小。
您明确地必须 "put down" 该约束到代码中。从这个角度来看,您现在拥有的代码很好。我什至认为它比其他答案中的建议更好,该建议试图将所有这些测试塞入一个 if 条件中。
你看,好的代码是易读易懂的。当然,"less code" 通常阅读起来更快,但有时 "bit more" 的代码比更短的版本更容易理解!
您可以使用||短路 OR 运算符以合并两个验证,如下所示:
public static double getInput(double minimumInput) {
Scanner scan = new Scanner(System.in);
double userInput =0;
System.out.print("Enter a number greater than " + minimumInput + ": ");
//Combine two vlidations using || operator
while (!scan.hasNextDouble() || ((userInput=scan.nextDouble()) < minimumInput)){
System.out.println("\nInvalid input.\n");
System.out.print("Enter a number greater than " + minimumInput + ": ");
} // end while
return userInput;
} // end getInput
有关运算符的更多详细信息,请参阅下面的link:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
当我收到用户的输入时,我想确保它是:
- 一个数字
- 大于最小值
我写了下面的代码来实现这个,但它看起来比它必须的更复杂。有没有办法合并问题输入的是一个数字吗,这个数字是否小于十,或者任何类似的两部分验证?
// function prompts user for a double greater than number passed in
// continues to prompt user until they input a number greater than
// the minimum number
public static double getInput(double minimumInput) {
Scanner scan = new Scanner(System.in);
double userInput;
System.out.print("Enter a number greater than " + minimumInput + ": ");
while (!scan.hasNextDouble()){
String garbage = scan.next();
System.out.println("\nInvalid input.\n");
System.out.print("Enter a number greater than " + minimumInput + ": ");
} // end while
userInput = scan.nextDouble();
while (userInput <= minimumInput) {
System.out.println("\nInvalid input.\n");
userInput = getInput(minimumInput);
}
return userInput;
} // end getInput
简单回答:没有。
你看,用户输入可以是任何东西。如果您不使用 "nextDouble()" 方法,您的代码甚至必须将字符串转换为数字。但是在java中没有办法说:这个东西是double,和它一定比其他一些值小。
您明确地必须 "put down" 该约束到代码中。从这个角度来看,您现在拥有的代码很好。我什至认为它比其他答案中的建议更好,该建议试图将所有这些测试塞入一个 if 条件中。
你看,好的代码是易读易懂的。当然,"less code" 通常阅读起来更快,但有时 "bit more" 的代码比更短的版本更容易理解!
您可以使用||短路 OR 运算符以合并两个验证,如下所示:
public static double getInput(double minimumInput) {
Scanner scan = new Scanner(System.in);
double userInput =0;
System.out.print("Enter a number greater than " + minimumInput + ": ");
//Combine two vlidations using || operator
while (!scan.hasNextDouble() || ((userInput=scan.nextDouble()) < minimumInput)){
System.out.println("\nInvalid input.\n");
System.out.print("Enter a number greater than " + minimumInput + ": ");
} // end while
return userInput;
} // end getInput
有关运算符的更多详细信息,请参阅下面的link: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html