我是 Java 编程新手,我正在尝试创建用户输入验证,以确保用户仅输入三个可能的字符串之一:哺乳动物、爬行动物、鸟类。但我热衷于尝试验证和创建循环。到目前为止我有这个:
public void validName() {
Scanner typeInput = new Scanner(System.in);
String [] type = {"Mammals", "Reptiles", "Birds"};
System.out.println("Enter Animal Type: ");
String atype = typeInput.next();
try {
if
(!Arrays.asList(type).contains(atype)){
System.out.println("Not a correct animal");
}
}
catch(Exception e){
System.out.println(e+"Plase add the correct Animal Type: (Mammals, Reptile, or Bird");
atype= typeInput.nextLine();}
while (atype.equalsIgnoreCase("Mammals") || atype.equalsIgnoreCase("Reptile") || atype.equalsIgnoreCase("Birds"));
{ System.out.println("Continue to next step");}
}
}
当我运行前面的代码时,我得到以下输出:
Please enter First Name
Cris
Please enter Last Name
Cruz
User logged In: Criz Cruz
Welcome to ZooOrganizer!
Enter Animal Type:
Cow
Not a correct animal
Continue to next step
------------------------------------------------------------------------
BUILD SUCCESS
-----------------------------------------------------------------------
我无法执行捕获异常,也无法执行循环来让用户再次输入动物类型。
public void validName() {
Scanner typeInput = new Scanner(System.in);
String [] type = {"Mammals", "Reptiles", "Birds"};
System.out.println("Enter Animal Type: ");
String atype = typeInput.next();
try {
if
(!Arrays.asList(type).contains(atype)){
System.out.println("Not a correct animal");
}
}
catch(Exception e){
System.out.println(e+"Plase add the correct Animal Type: (Mammals, Reptile, or Bird");
atype= typeInput.nextLine();}
while (atype.equalsIgnoreCase("Mammals") || atype.equalsIgnoreCase("Reptile") || atype.equalsIgnoreCase("Birds"));
{ System.out.println("Continue to next step");}
}
}
最佳答案
你仔细想想,你编码的提示其实是相当残酷的。它不会告知用户预期的输入内容。您也可以显示如下提示:
Hey, enter an Animal Type and if you guess it right
you get two free OH-Henry Bars (yum yum): -->
提前了解用户的要求,如果可以的话,使输入尽可能简单。如果您这样做,那么该用户可能产生的错误几乎完全消除,例如:
Enter an Animal Type (Mammals, Reptiles, Birds): -->
现在用户可以看到您期望的输入。然而,这仍然存在您的代码需要处理和处理的问题,例如拼写错误、字母大小写不正确、未输入单词等。在我看来,必须编写单词实际上是一种痛苦爬行动物进入诸如控制台应用程序之类的东西,这就是为什么我会避免使用这些应用程序,你知道:
Enter the full path and file name to your Database located within
the Windows Documents folder: -->
是的,我不这么认为......下一个应用程序。
当您有多个可以输入的项目时,请使用菜单系统。这样用户就可以看到可用的选项,并且只需要输入所需菜单项的单个字母或数字,例如:
Select an Animal Type (1-3):
1) Mammal
2) Reptiles
3) Birds
4) Quit
Menu Choice: -->
这样做还可以减少执行有效性所需的代码量。输入的菜单选项是否为整数,该条目是否大于或等于 1,是否小于或等于 4。如果不是,则告诉用户无效并再次循环。以下是您可以如何使用当前方案执行此操作:
String ls = System.lineSeparator();
Scanner typeInput = new Scanner(System.in);
String[] type = {"Mammals", "Reptiles", "Birds"};
String selectedAnimalType = "";
String atype = "";
// Start a prompt WHILE loop...
while (atype.equals("")) {
/* Display a Menu. Doing things this way doesn't leave
the User in the dark as to what is required for input. */
System.out.print("Select an Animal Type (1-3): " + ls
+ "1) Mammal" + ls + "2) Reptiles" + ls
+ "3) Birds" + ls + "4) Quit" + ls
+ "Menu Choice: --> ");
// Get User input...
atype = typeInput.nextLine();
// Is the Input a Valid menu choice?
if (!atype.matches("\\d") || Integer.valueOf(atype) < 1 || Integer.valueOf(atype) > 4) {
/* If it's not a string representation of a Integer numerical value OR
if it's a numerical value less than 1 OR if it's a numerical value
greater than 4 */
System.out.println("Invalid entry! Please try again..." + ls);
atype = ""; // Make atype equal null string ("") to continue WHILE loop
}
// Otherwise, was the menu choice the numerical value 4 to quit?
else if (Integer.valueOf(atype) == 4) {
// Yes, it was...
System.out.println("Quiting... Bye-Bye");
System.exit(0); // Quit (end) Application.
}
}
// Prompt loop successful...continue on with code.
/* Get the proper name for the Animal Type from the 'type' Array
based on the menu choice (numerical value minus 1) so as to get
the desired array index value. */
selectedAnimalType = type[Integer.valueOf(atype) - 1];
/* The condition for the below WHILE loop is redundant since we
would NEVER get this far unless a menu choice for either Mammal,
Reptiles, or Birds, was made, so don't bother using it. Do something
similar as to what was done in the first prompt loop above. */
while (atype.equalsIgnoreCase("Mammals") || atype.equalsIgnoreCase("Reptile") || atype.equalsIgnoreCase("Birds")) {
System.out.println("Continue to next step");
// ........................................
}
关于java - 用户字符串输入验证和异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57280935/