您可以尝试使用user.equals(“1”)而不是Integer.parseInt(user)== 1.或者假设异常java.lang.NumberFormatException有效并捕获该异常并以正确的方式对待它,例如:
System.out.println("where would you like to send the following item: ");
System.out.println("1. groceries, 2. savings, 3. utilities");
System.out.print("Enter list name or list number to send: ");
String user = user_input.nextLine();
try {
if (user.toLowerCase().equals("groceries") || Integer.parseInt(user) == 1) {
System.out.println("item was sent to the grocery list");
}else if(user.toLowerCase().equals("savings") || Integer.parseInt(user) == 2) {
System.out.println("item was sent to the savings list");
}else if(user.toLowerCase().contains("utilities") || Integer.parseInt(user) == 3) {
System.out.println("item was sent to the utilities list");
}else{
System.out.println("item is not in any category");
}
} catch (NumberFormatException nfe) {
System.out.println("item is not in any category");
}
更新:
System.out.println("where would you like to send the following item: ");
System.out.println("1. groceries, 2. savings, 3. utilities");
System.out.print("Enter list name or list number to send: ");
String user = user_input.nextLine();
if (user.toLowerCase().equals("groceries") || user.equals("1")) {
System.out.println("item was sent to the grocery list");
}else if(user.toLowerCase().equals("savings") || user.equals("2")) {
System.out.println("item was sent to the savings list");
}else if(user.toLowerCase().contains("utilities") || user.equals("3")) {
System.out.println("item was sent to the utilities list");
}else{
System.out.println("item is not in any category");
}