【发布时间】:2021-02-11 12:49:20
【问题描述】:
我正在创建一个 java 程序,它会询问用户他们想要在比萨上添加什么配料/配料的问题。获得用户输入后,程序将打印一份成分列表。我刚开始写程序,所以还没有完成。我遇到的问题是验证用户输入。例如,如果选择是“a”或“b”并且用户输入“c”,我不确定如何让程序强制用户输入正确的选择。使用我到目前为止的代码,它只显示一条错误消息,例如“无效选择。请输入 a 或 b”,但只是转到下一个问题。我如何确保用户选择了正确的选项,然后如果用户没有输入有效的选项则循环返回?
这是我目前所拥有的:
package pizza.program;
import java.util.Scanner;
public class PizzaProgram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String CrustType;
String Sauce;
String Cheese;
String Ingredient1;
System.out.println("_______________________________________________________");
System.out.println("Please choose one crust option: ");
System.out.println("\n");
System.out.println("a. regular crust b. gluten-free crust");
System.out.println("\n");
System.out.printf("Enter choice: ");
CrustType= input.next();
if(CrustType.equals("a")) {
System.out.println("You chose: regular crust");
}
else if(CrustType.equals("b")) {
System.out.println("You chose: gluten-free crust");
}
else {
System.out.println("Invalid Selection. Please enter a or b");
System.out.println("Enter choice: ");
CrustType = input.next();
}
System.out.println("_______________________________________________________");
System.out.println("Please choose one sauce option: ");
System.out.println("\n");
System.out.println("a. red sauce b. no red sauce");
System.out.println("\n");
System.out.printf("Enter choice: ");
Sauce= input.next();
if(Sauce.equals("a")) {
System.out.println("You chose: red sauce");
}
else if(Sauce.equals("b")) {
System.out.println("You chose: no red sauce");
}
else {
System.out.println("Invalid Input. Please enter a or b");
System.out.println("Enter choice: ");
Sauce = input.next();
}
System.out.println("_______________________________________________________");
System.out.println("Would you like to add cheese to the pizza? (Y/N): ");
System.out.println("\n");
System.out.printf("Enter choice: ");
Cheese = input.next();
if(Cheese.equals("Y")) {
System.out.println("You chose to add cheese to the pizza.");
}
else if(Sauce.equals("N")) {
System.out.println("You chose NOT to add cheese to the pizza.");
}
else {
System.out.println("Invalid Input. Please enter Y or N");
Cheese = input.next();
}
System.out.println("_______________________________________________________");
System.out.println("Please choose one ingredient option: ");
System.out.println("\n");
System.out.println("a. diced onion b. diced green pepper c. pepperoni");
System.out.println("d. sliced mushrooms e. diced jalapenos f. sardines");
System.out.println("g. pineapple chunks h. tofu i. ham chunks");
System.out.println("j. dry red pepper k. dry basil");
System.out.println("\n");
System.out.printf("Enter choice: ");
Ingredient1 = input.next();
if(Ingredient1.equals("Y")) {
System.out.println("You chose to add cheese to the pizza.");
}
else if(Ingredient1.equals("N")) {
System.out.println("You chose NOT to add cheese to the pizza.");
}
else {
System.out.println("Invalid Input. Please enter Y or N");
Ingredient1 = input.next();
}
}
}
【问题讨论】:
-
简而言之,做一个循环,在你的输入有效之前不要退出它。
标签: java loops if-statement while-loop do-while