创建披萨配料Java程序

问题描述

我正在创建一个Java程序,该程序将询问用户有关他们想要比萨饼上的哪种配料/配料的问题。收到用户输入后,程序将打印配料表。我刚刚开始编写程序,所以还没有结束。我遇到的麻烦是验证用户输入。例如,如果选择是“ a”或“ b”,并且用户输入“ c”,则我不确定如何使程序强制用户输入正确的选择。使用到目前为止的代码,它只会显示一条错误消息,例如“ Invalid Selection。请输入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();
    }
   
    
}
}    

解决方法

尝试一下:

do{        
    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");
    }
    }while(!CrustType.equals("a") && !CrustType.equals("b"));