如何实现一个way循环以验证IF IF输入(Java)



我是一个新的Java学生。我是Olderrrrr,一生我无法弄清楚这一点。我们尚未完成数组,所以这不是问题的一部分。

此程序应在一个例外情况下运行。我需要实现一个段循环,以验证这种风味和/或尺寸的选择。

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" + ": $%.2fn",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" + ": $%.2fn",totalCost);
}
}

但是,无论我放在哪里或如何设置它,它都会打破其他内容,并且只会经过一次迭代。

任何建议将不胜感激!

对其进行了修改:

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: $%.2fn", totalCost );
    }
}

最新更新