Javascript 如何在java中不重复验证
我正在写一个程序来计算房子里油漆房间的价格。 该程序会向用户询问几个问题,用户必须输入有关该主题的特定答案。在不接受0和负数的情况下,程序还必须实施验证:Javascript 如何在java中不重复验证,javascript,java,validation,methods,Javascript,Java,Validation,Methods,我正在写一个程序来计算房子里油漆房间的价格。 该程序会向用户询问几个问题,用户必须输入有关该主题的特定答案。在不接受0和负数的情况下,程序还必须实施验证: Scanner keyboard = new Scanner(System.in); System.out.println("Number of rooms to be painted?"); noRooms = keyboard.nextDouble(); while (noRooms <= 0) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Number of rooms to be painted?");
noRooms = keyboard.nextDouble();
while (noRooms <= 0) {
System.out.println("Invalid Input for the number of rooms. Please enter in the appropriate number:");
noRooms = keyboard.nextDouble();
System.out.println("Number of sqaure feet of wall space in each room?");
wallSpace = keyboard.nextDouble();
while (wallSpace <= 0) {
System.out.println("Invalid Input for the number of square feet of wall space. Please enter in the appropriate number:");
wallSpace = keyboard.nextDouble();
没有出现错误,但当我输入“正确”或“不正确”数据时,程序不会执行任何操作。还有其他方法调用该方法吗?我建议您提取一个方法来提示输入值,并调用它两次。大概
static double getValidValue(Scanner sc, String initial, String msg) {
System.out.println(initial);
double v = sc.nextDouble();
while (v <= 0) {
System.out.printf("Invalid input for the %s. "
+ "Please enter in the appropriate number:%n", msg);
}
return v;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double noRooms = getValidValue(keyboard,
"Number of rooms to be painted?", "number of rooms");
double wallSpace = getValidValue(keyboard,
"Number of sqaure feet of wall space in each room?",
"number of square feet of wall space");
// ... noRooms and wallSpace are > 0.
}
静态双getValidValue(扫描程序sc、字符串首字母、字符串msg){
系统输出打印项次(首字母);
双v=sc.nextDouble();
而(v 0。
}
有一个方法,它接受扫描仪、提示、错误消息和错误边界,并返回请求的输入。我做了类似的操作,但在运行程序并输入数据时没有显示输出。@user3622970否,您所做的并不完全相似。请注意,在提示输入之前,您忘记打印消息。
private static double validation(Scanner keyboard) {
double v = keyboard.nextDouble();
while (v <= 0) {
System.out.println("Invalid Input: Please enter in the appropriate number:");
}
return v;
}
System.out.println("Number of rooms to be painted:");
noRoom= keyboard.nextDouble();
noRoom= validation(keyboard);
static double getValidValue(Scanner sc, String initial, String msg) {
System.out.println(initial);
double v = sc.nextDouble();
while (v <= 0) {
System.out.printf("Invalid input for the %s. "
+ "Please enter in the appropriate number:%n", msg);
}
return v;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double noRooms = getValidValue(keyboard,
"Number of rooms to be painted?", "number of rooms");
double wallSpace = getValidValue(keyboard,
"Number of sqaure feet of wall space in each room?",
"number of square feet of wall space");
// ... noRooms and wallSpace are > 0.
}