英文:
How should I handle invalid user input?
问题
以下是翻译好的内容:
import java.util.Scanner;
public class ABaybayanAssignment3 {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("为了我的餐厅选择程序,我需要编写一个使用决策结构的程序:在编译和运行时出现了一些错误,比如:");
System.out.println("Exception in thread \"main\" java.util.InputMismatchException");
System.out.println(" at java.base/java.util.Scanner.throwFor(Scanner.java:939)");
System.out.println(" at java.base/java.util.Scanner.next(Scanner.java:1594)");
System.out.println(" at java.base/java.util.Scanner.nextBoolean(Scanner.java:1893)");
System.out.println(" at ABaybayanAssignment3.main(ABaybayanAssignment3.java:12)");
System.out.println("我想知道我该如何修改我的代码以修复它。以下是我的代码:");
System.out.println("import java.util.Scanner;");
System.out.println("public class ABaybayanAssignment3 {");
System.out.println(" public static void main(String[] args) {");
System.out.println(" Scanner scnr = new Scanner(System.in);");
System.out.println(" System.out.println(\"对以下问题,请输入yes或no:\");");
System.out.println(" System.out.println(\"您的团队中是否有人是素食者?\");");
System.out.println(" boolean Vegetarian = scnr.nextBoolean();");
System.out.println(" System.out.println(\"您的团队中是否有人是纯素食者?\");");
System.out.println(" boolean vegan = scnr.nextBoolean();");
System.out.println(" System.out.println(\"您的团队中是否有人需要无麸质食品?\");");
System.out.println(" boolean glutenFree = scnr.nextBoolean();");
System.out.println(" System.out.println(\"以下是您的餐厅选择:\");");
System.out.println(" if (Vegetarian || vegan || glutenFree) {");
System.out.println(" System.out.println(\"Corner Cafe\");");
System.out.println(" }");
System.out.println(" if (!Vegetarian && !vegan && !glutenFree) {");
System.out.println(" System.out.println(\"Joe's Gourmet Burgers\");");
System.out.println(" }");
System.out.println(" if (!vegan) {");
System.out.println(" System.out.println(\"Main Street Pizza Company\");");
System.out.println(" }");
System.out.println(" if (!vegan && !glutenFree) {");
System.out.println(" System.out.println(\"Mama's Fine Italian\");");
System.out.println(" }");
System.out.println(" }");
System.out.println("}");
}
}
英文:
For my Restaurant selector program, I am required to write a program that uses decision structures.: I'm getting a couple of errors when I compile and run it such as:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextBoolean(Scanner.java:1893)
at ABaybayanAssignment3.main(ABaybayanAssignment3.java:12)
I was wondering what could I do to my code to fix it. My code can be found below
import java.util.Scanner;
public class ABaybayanAssignment3 {
public static void main(String[]args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Type yes or no for the following questions:");
System.out.println("Is anyone in your party vegetarian?");
boolean Vegetarian = scnr.nextBoolean();
System.out.println("Is anyone in your party vegan?");
boolean vegan = scnr.nextBoolean();
System.out.println("Is anyone in your party gluten free?");
boolean glutenFree = scnr.nextBoolean();
System.out.println("Here are your restaurant choices:");
if(Vegetarian || vegan || glutenFree ) {
System.out.println("Corner Cafe");
}
if(!Vegetarian && !vegan && !glutenFree) {
System.out.println("Joe's Gourmet Burgers");
}
if(!vegan) {
System.out.println("Main Street Pizza Company");
}
if(!vegan && !glutenFree) {
System.out.println("Mama's Fine Italian");
}
}
}
答案1
得分: 0
程序编译并且运行没有任何错误。
你的程序问题在于你在运行时将Vegetarian
、vegan
、glutenFree
这些布尔变量赋值为yes
和no
,这是错误的。
另外,与C++不同,在Java中不能将数值赋值给布尔变量 - 只能使用true或false。
此外,将
System.out.println("Type yes or no for the following questions:");
改为
System.out.println("Type true or false for the following questions:");
英文:
The program compiles and runs without any error.
The problem with your program is you assign yes
and no
to the boolean variable Vegetarian
, vegan
, glutenFree
at runtime, which is wrong.
Also Unlike C++, a numerical value cannot be assigned to a boolean variable in Java – only true or false can be used.
Furthermore, change
System.out.println("Type yes or no for the following questions:");
TO
System.out.println("Type true or false for the following questions:");
答案2
得分: 0
主要问题我认为是您没有处理无效的输入错误。最好是捕捉这些异常并提示用户输入有效的内容。或者,您还可以添加一个默认值,在用户未输入任何内容的情况下将布尔值设为默认值(当然,在程序开始时要明确指定)。
类似这样的代码应该有助于表达我的意思:
public class ABaybayanAssignment3 {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Type yes or no for the following questions:");
boolean Vegetarian = requestBooleanInput(scnr, "Is anyone in your party vegetarian?");
boolean vegan = requestBooleanInput(scnr, "Is anyone in your party vegan?");
boolean glutenFree = requestBooleanInput(scnr, "Is anyone in your party gluten free?");
System.out.println("Here are your restaurant choices:");
if (Vegetarian || vegan || glutenFree) {
System.out.println("Corner Cafe");
}
if (!Vegetarian && !vegan && !glutenFree) {
System.out.println("Joe's Gourmet Burgers");
}
if (!vegan) {
System.out.println("Main Street Pizza Company");
}
if (!vegan && !glutenFree) {
System.out.println("Mama's Fine Italian");
}
}
public static boolean requestBooleanInput(Scanner scanner, String message){
boolean value = false;
boolean validInput = false;
while (!validInput){
System.out.println(message);
try {
value = scanner.nextBoolean();
validInput = true;
} catch (Exception e) {
System.out.println("You entered an invalid input '" + scanner.next() + "', please enter only 'true' or 'false'");
}
}
return value;
}
}
英文:
The main issue I think is that you do not handle invalid input errors. Preferably you catch these exceptions and prompt the user to enter a valid input. Optionally, you could also add a default value, that the booleans are set to in the case of nothing being entered by the user (of course specify this at the start of the program).
Something like this should help convey what I mean:
public class ABaybayanAssignment3 {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Type yes or no for the following questions:");
boolean Vegetarian = requestBooleanInput(scnr,"Is anyone in your party vegetarian?" );
boolean vegan = requestBooleanInput(scnr,"Is anyone in your party vegan?");
boolean glutenFree = requestBooleanInput(scnr,"Is anyone in your party gluten free?");
System.out.println("Here are your restaurant choices:");
if (Vegetarian || vegan || glutenFree) {
System.out.println("Corner Cafe");
}
if (!Vegetarian && !vegan && !glutenFree) {
System.out.println("Joe's Gourmet Burgers");
}
if (!vegan) {
System.out.println("Main Street Pizza Company");
}
if (!vegan && !glutenFree) {
System.out.println("Mama's Fine Italian");
}
}
public static boolean requestBooleanInput(Scanner scanner, String message){
boolean value = false;
boolean validInput = false;
while (!validInput){
System.out.println(message);
try {
value = scanner.nextBoolean();
validInput = true;
} catch (Exception e) {
System.out.println("You entered an invalid input '"+scanner.next()+"', please enter only 'true' or 'false'");
}
}
return value;
}
}
答案3
得分: 0
我为您的程序提供了一个解决方案。我使用了布尔值并且使用了true/false
来替代yes/no
。从逻辑角度来看,它们都是相同的。所以这是我的程序。
public class testing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type true or false for the following questions:");
System.out.println("Is anyone in your party vegetarian?\n");
boolean Vegetarian = sc.nextBoolean();
System.out.println("Is anyone in your party vegan?\n");
boolean vegan = sc.nextBoolean();
System.out.println("Is anyone in your party gluten free?\n");
boolean glutenFree = sc.nextBoolean();
System.out.println("Here are your restaurant choices:");
if(Vegetarian == true && vegan == true && glutenFree == true) {
System.out.println("Corner Cafe");
} else if(Vegetarian == false && vegan == false && glutenFree == false) {
System.out.println("Joe's Gourmet Burgers");
} else if(vegan == false) {
System.out.println("Main Street Pizza Company");
} else if(vegan == false && glutenFree == false) {
System.out.println("Mama's Fine Italian");
}
}
}
而我的输出显示如下,
Type true or false for the following questions:
Is anyone in your party vegetarian?
true
Is anyone in your party vegan?
true
Is anyone in your party gluten free?
true
Here are your restaurant choices:
Corner Cafe
我稍微修改了程序,以使其更加简化。由于使用了布尔值,因此唯一的选项是true或false。除此之外,if-else 验证也稍作修改,以适应true/false
的输入。所以我希望这回答了您的问题。
英文:
I have a solution for your program. I used Boolean and used true/false
instead of yes/no
. Both of them are same in terms of logic. So here's my program.
public class testing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type true or false for the following questions:");
System.out.println("Is anyone in your party vegetarian?\n");
boolean Vegetarian = sc.nextBoolean();
System.out.println("Is anyone in your party vegan?\n");
boolean vegan = sc.nextBoolean();
System.out.println("Is anyone in your party gluten free?\n");
boolean glutenFree = sc.nextBoolean();
System.out.println("Here are your restaurant choices:");
if(Vegetarian == true && vegan == true && glutenFree == true) {
System.out.println("Corner Cafe");
} else if(Vegetarian == false && vegan == false && glutenFree == false) {
System.out.println("Joe's Gourmet Burgers");
} else if(vegan == false) {
System.out.println("Main Street Pizza Company");
} else if(vegan == false && glutenFree == false) {
System.out.println("Mama's Fine Italian");
}
}
}
And my output shows this,
Type true or false for the following questions:
Is anyone in your party vegetarian?
true
Is anyone in your party vegan?
true
Is anyone in your party gluten free?
true
Here are your restaurant choices:
Corner Cafe
I have changed the program abit so as to make it easier of course. Due to usage of Boolean, then the only option is true or false. Apart from that, the if-else validation is also modified abit to complement the true/false
input. So I hope that answers your question
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论