2017-10-03 3 views
-3

Je suis une nouvelle étudiante java. Je suis olderrrrr et pour la vie de moi je ne suis pas capable de comprendre cela. Nous n'avons pas encore fait de tableaux, ce qui ne fait pas partie du problème.Comment implémenter une boucle while pour la validation avec les instructions if else pour l'entrée (java)

Ce programme s'exécute comme il se doit avec une exception. J'ai besoin de mettre en place une boucle while pour valider les choix de saveur et/ou de taille.

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

Mais peu importe où je mets ou comment je l'ai mis en place, il brise quelque chose d'autre et il ne passer par une seule itération.

Un conseil serait apprécié!

+1

Java et JavaScript sont des langues distinctes. (Question mise à jour et extrait JS supprimé.) – nnnnnn

+1

N'utilisez pas un extrait 'Javascript' pour afficher le code' Java' ... –

+0

@StevenJeffries 'nextLine' n'inclut pas les fins de ligne.:"Cette méthode renvoie le reste de la ligne en cours , à l'exclusion de tout séparateur de ligne à la fin. " Selon http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine--docs: –

Répondre

-2

modifia un peu:

public static void main(final String[] args) 
{ 
    // `Scanner` should be closed... this will close it up in the end... 
    try (final Scanner scnr = new Scanner(System.in)) 
    { 
     // variable declarations 
     double totalCost = 0.0; // accumulator to hold value of items selected 
     String flavor;   // flavor choice entered by user 
     String size;   // size choice entered by user 
     String addAnother;  // Holds 'yes' or 'no' 

     // infinte loop with a label, will use `break label` to get out of it... 
     addAnotherLoop: while (true) 
     { 
      // Total cost based on calculation of size * flavor 
      // moved here, because the initialization 
      double pricePerInch = 0.0; // cost per inch 
      double inches = 0.0;  // inches per size 
      double cost = 0.0;   // cost calculation base on flavor and size selections 

      // ask user for flavour input 
      System.out.println("Enter the flavor you'd like:\n" + 
           "(plain, caramel, chocolate, raspberry, or strawberry)"); 
      do 
      { 
       flavor = scnr.nextLine(); 
       if ("plain".equalsIgnoreCase(flavor)) 
       {    // plain cheesecake 
        pricePerInch = 0.50; 
       } 
       else if ("caramel".equalsIgnoreCase(flavor)) 
       { // caramel cheesecake 
        pricePerInch = 0.75; 
       } 
       else if ("chocolate".equalsIgnoreCase(flavor)) 
       { // chocolate cheesecake 
        pricePerInch = 0.85; 
       } 
       else if ("raspberry".equalsIgnoreCase(flavor)) 
       { // raspbery cheesecake 
        pricePerInch = 1.15; 
       } 
       else if ("strawberry".equalsIgnoreCase(flavor)) 
       { // strawberry cheesecake 
        pricePerInch = 1.25; 
       } 
       else 
       { 
        System.out.println("That's not a valid flavor, please enter: plain," + 
             "caramel, chocolate, raspberry, or strawberry."); 
       } 
      } 
      // if no valid flavour was entered, `pricePerInch` is still 0... 
      while (0.0 == pricePerInch); 

      // ask user for size input 
      System.out.println("Enter the size you'd like:\n(bite size, small, or large)"); 
      do 
      { 
       size = scnr.nextLine(); 

       // Prompt user for a size selection 
       if ("bite size".equalsIgnoreCase(size)) 
       {  // bite size 
        inches = 3;      // size in inches 
       } 
       else if ("small".equalsIgnoreCase(size)) 
       {   // small 
        inches = 6;      // size in inches 
       } 
       else if ("large".equalsIgnoreCase(size)) 
       {  // large 
        inches = 9;      // size in inches 
       } 
       else 
       { 
        System.out.printf("I'm sorry %s isn't one our size options.\n" + 
             "Please choose: bite size, small, or large.\n", 
             size); 
       } 
      } 
      // if no valid size was entered, `inches` is still 0... 
      while (0.0 == inches); 

      // calculate cheesecake cost base on flavor and size selections 
      cost = pricePerInch * inches; 
      System.out.printf("%s %s cheesecake: $%.2f%n", flavor, size, cost); 

      // add cheesecake cost to accumulator totalCost 
      totalCost += cost; 

      // Prompt user if they want to add to their order? 
      System.out.println("Would you like to add another cheesecake to your order?"); 
      do 
      { 
       System.out.print("Enter yes or no: "); 
       addAnother = scnr.nextLine(); 
       // if no, exit the outer loop 
       if ("no".equalsIgnoreCase(addAnother)) 
       { 
        break addAnotherLoop; 
       } 
      } 
      // if not yes, continue the loop 
      while (! "yes".equals(addAnother)); 
     } 

     // Display total sales 
     System.out.printf("The total cost for your order is: $%.2f\n", totalCost); 
    } 
}