mercredi 28 octobre 2020

Creating pizza ingredients java program

I am creating a java program which will ask a user questions regarding what ingredients/toppings they would like on a pizza. After getting the user input, the program will print an ingredients list. I just started writing the program so its not finished. What I am having trouble with is validating the user input. for instance If the choices are "a" or "b" and the user enters "c", im not sure how to make the program force the user to enter a proper choice. With the code I have so far, It just displays an error message such as "Invalid Selection. Please enter a or b" but just moves on to the next question. How do I make sure the user is choosing a proper option then loop back around if the user doesnt enter in a valid choice?

Heres what I have so far:

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();
    }
   
    
}

}

Aucun commentaire:

Enregistrer un commentaire