如何验证 Java 中的用户输入?
How to validate user input in Java?
我正在尝试获取用户输入并检查它是“1”还是“2”,如果不是则显示错误消息。即使输入正确,我仍然收到错误消息。
Scanner user_input = new Scanner(System.in);
String choice = "";
// Input Validation
do {
// Read user choice
choice = user_input.nextLine();
if (!choice.equals("1") || !choice.equals("2"))
System.out.println("Invalid input. Give new value");
}while (!choice.equals("1") || !choice.equals("2"));```
您的条件不正确。
如果需要同时消除 1 和 2,请使用逻辑与。
我想你想实现这个
do {
choice = user_input.nextLine();
if (!choice.equals("1") && !choice.equals("2"))
System.out.println("Invalid input. Give new value");
} while (!choice.equals("1") && !choice.equals("2"));
此外,为了消除冗余并提高代码的可读性,请考虑将验证逻辑移除到单独的方法中。
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String choice = "";
do {
choice = user_input.nextLine();
if (isValid(choice))
System.out.println("Invalid input. Give new value");
} while (isValid(choice));
System.out.println("Your input is valid: " + choice);
}
private static boolean isValid(String choice) {
return !choice.equals("1") && !choice.equals("2");
}
if (!choice.equals("1") || !choice.equals("2"))
没有意义
我比较简单,所以我总是喜欢做一些简单的检查,例如...
System.out.println(!true || !false); // !"1".equals("1") || !"1".equals("2")
System.out.println(!false || !true); // !"2".equals("1") || !"2".equals("2")
System.out.println(!false || !false); // !"3".equals("1") || !"3".equals("2")
打印
true // Show error message ... this is wrong, `1` is valid
true // Show error message ... this is wrong, `2` is valid
true // Show error message ... this is correct
这是明显错误的,第一个语句,我们当不是1
或2
时,打印一条错误消息
因此,相反,我们应该将 ||
两边的结果取反,例如...
System.out.println(!(true || false)); // !("1".equals("1") || "1".equals("2"))
System.out.println(!(false || true)); // !("2".equals("1") || "2".equals("2"))
System.out.println(!(false || false)); // !("3".equals("1") || "3".equals("2"))
打印...
false // Don't show error message ... `1` is valid
false // Don't show error message ... `2` is valid
true // Show error message ... `3` is invalid
好的,这看起来更好
我还会在此过程中简化退出条件...
Scanner user_input = new Scanner(System.in);
String choice = "";
boolean isValidChoice = false;
// Input Validation
do {
// Read user choice
choice = user_input.nextLine();
if (!(choice.equals("1") || choice.equals("2"))) {
System.out.println("Invalid input. Give new value");
} else {
isValidChoice = true;
}
} while (!isValidChoice);
System.out.println("Good choice!");
条件中存在逻辑错误。应该有“&&”而不是“||”。
感谢@tgdavies。
像这样...
String choice = "";
// Input Validation
do {
// Read user choice
choice = user_input.nextLine();
if (!choice.equals("1") && !choice.equals("2"))
System.out.println("Invalid input. Give new value");
}while (!choice.equals("1") && !choice.equals("2"));
如果输入有效,您可以在循环中间转义。
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String choice = "";
// Input Validation
while (true) {
// Read user choice
choice = user_input.nextLine();
if (choice.equals("1") || choice.equals("2"))
break;
System.out.println("Invalid input. Give new value");
}
}
我正在尝试获取用户输入并检查它是“1”还是“2”,如果不是则显示错误消息。即使输入正确,我仍然收到错误消息。
Scanner user_input = new Scanner(System.in);
String choice = "";
// Input Validation
do {
// Read user choice
choice = user_input.nextLine();
if (!choice.equals("1") || !choice.equals("2"))
System.out.println("Invalid input. Give new value");
}while (!choice.equals("1") || !choice.equals("2"));```
您的条件不正确。 如果需要同时消除 1 和 2,请使用逻辑与。 我想你想实现这个
do {
choice = user_input.nextLine();
if (!choice.equals("1") && !choice.equals("2"))
System.out.println("Invalid input. Give new value");
} while (!choice.equals("1") && !choice.equals("2"));
此外,为了消除冗余并提高代码的可读性,请考虑将验证逻辑移除到单独的方法中。
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String choice = "";
do {
choice = user_input.nextLine();
if (isValid(choice))
System.out.println("Invalid input. Give new value");
} while (isValid(choice));
System.out.println("Your input is valid: " + choice);
}
private static boolean isValid(String choice) {
return !choice.equals("1") && !choice.equals("2");
}
if (!choice.equals("1") || !choice.equals("2"))
没有意义
我比较简单,所以我总是喜欢做一些简单的检查,例如...
System.out.println(!true || !false); // !"1".equals("1") || !"1".equals("2")
System.out.println(!false || !true); // !"2".equals("1") || !"2".equals("2")
System.out.println(!false || !false); // !"3".equals("1") || !"3".equals("2")
打印
true // Show error message ... this is wrong, `1` is valid
true // Show error message ... this is wrong, `2` is valid
true // Show error message ... this is correct
这是明显错误的,第一个语句,我们当不是1
或2
时,打印一条错误消息
因此,相反,我们应该将 ||
两边的结果取反,例如...
System.out.println(!(true || false)); // !("1".equals("1") || "1".equals("2"))
System.out.println(!(false || true)); // !("2".equals("1") || "2".equals("2"))
System.out.println(!(false || false)); // !("3".equals("1") || "3".equals("2"))
打印...
false // Don't show error message ... `1` is valid
false // Don't show error message ... `2` is valid
true // Show error message ... `3` is invalid
好的,这看起来更好
我还会在此过程中简化退出条件...
Scanner user_input = new Scanner(System.in);
String choice = "";
boolean isValidChoice = false;
// Input Validation
do {
// Read user choice
choice = user_input.nextLine();
if (!(choice.equals("1") || choice.equals("2"))) {
System.out.println("Invalid input. Give new value");
} else {
isValidChoice = true;
}
} while (!isValidChoice);
System.out.println("Good choice!");
条件中存在逻辑错误。应该有“&&”而不是“||”。
感谢@tgdavies。
像这样...
String choice = "";
// Input Validation
do {
// Read user choice
choice = user_input.nextLine();
if (!choice.equals("1") && !choice.equals("2"))
System.out.println("Invalid input. Give new value");
}while (!choice.equals("1") && !choice.equals("2"));
如果输入有效,您可以在循环中间转义。
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String choice = "";
// Input Validation
while (true) {
// Read user choice
choice = user_input.nextLine();
if (choice.equals("1") || choice.equals("2"))
break;
System.out.println("Invalid input. Give new value");
}
}