lundi 2 octobre 2017

How to implement a while loop for validation with if else statements for input (java)

I am a new java student. I am olderrrrr and for the life of me I've not be able to figure this out. We have not done arrays yet so that isn't part of the problem.

This program runs as it should with one exception. I need to implement a while loop to validate that the flavor and/or size choices.

import java.util.Scanner;

public class Cheesecake2 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                //variable declarations
                
                                
                //Total cost  based on calculation of size * flavor
                double pricePerInch = 0.0;      //cost per inch 
                double cost = 0.0;                      //cost calculation base on flavor and size selections
                double inches = 0.0;            //inches per size
                double totalCost = 0.0;         //accumulator to hold value of items selected
                
                
                  Scanner scnr = new Scanner(System.in);
              String flavor = "";       //flavor choice entered by user
              String size = "";         //size choice entered by user
              String addAnother;  //Holds 'yes' or 'no'
              
              
              do {        
                System.out.println("Enter the flavor you'd like (plain, caramel, chocolate, raspberry, or strawberry:)"); 
                flavor = scnr.nextLine();
                        
                //Calculate users total cost based on flavor and size choices
                if (flavor.equalsIgnoreCase("plain")) {                         //plain cheesecake 
                        pricePerInch = 0.50;                            
                }
                else if (flavor.equalsIgnoreCase("caramel")) {  //caramel cheesecake                                                    
                        pricePerInch = 0.75;
                }
                else if (flavor.equalsIgnoreCase("chocolate")) {        //chocolate cheesecake
                        pricePerInch = 0.85;
                }
                else if (flavor.equalsIgnoreCase("raspberry")) {        //raspbery cheesecake
                        pricePerInch = 1.15;
                }
                else if (flavor.equalsIgnoreCase("strawberry")) {       //strawberry cheesecake
                        pricePerInch = 1.25;
                }
                else {
                        System.out.println("That's not a valid flavor, please enter: plain, caramel, chocolate, raspberry, or strawberry.");
                          }
                
                while (flavor != ("plain") || flavor != ("caramel") ||flavor != ("chocolate") ||flavor != ("raspberry") ||flavor != ("strawberry")); {
                        System.out.println("Please enter a valid flavor choice");
                        flavor = scnr.nextLine();
                }
                    
                //ask user for size input
                     System.out.println("Enter the size you'd like (bite size, small, or large)");  
                     size = scnr.nextLine();  
                      
                     //Prompt user for a size selection
                     if (size.equalsIgnoreCase("bite size")) {          //bite size                                             
                        inches = 3;                                             //size in inches
                }
                     else if (size.equalsIgnoreCase("small")) {                 //small                                         
                         inches = 6;                                            //size in inches
                }
                     else if (size.equalsIgnoreCase("large")) {         //large
                          inches = 9;                                           //size in inches
                     }
                     else {
                         System.out.println("I'm sorry " + size + " isn't one our size options. Please choose: bite size, small, or large.");
                     }
                      
                     cost = pricePerInch * inches;  //calculate cheesecake cost base on flavor and size selections
                     System.out.printf(flavor + " " + size + " cheesecake" + ": $%.2f\n",cost);
                      
                      totalCost += cost;                        //add cheesecake cost to accumulator totalCost
              
                      //Prompt user if they want to add to their order?
                      System.out.println("Would you like to add another cheesecake to your order? Enter yes or no");
                      addAnother = scnr.nextLine();
              
              } while (addAnother.equals("yes"));
              
              //Display total sales
              System.out.printf("The total cost for your order is" + ": $%.2f\n",totalCost);
}
}

But no matter where I put it or how I set it up, it breaks something else and it will only go through a single iteration.

Any advice would be appreciated!

Aucun commentaire:

Enregistrer un commentaire