我从用户那里得到选择后就不会应用我的价格

时间:2018-07-09 22:38:19

标签: java subtotal totals

我是java的新手,我想弄清楚为什么代码没有分配从用户那里得到的信息,然后在最后乘以它,以便我可以打印出所有收据给客户的收据。他们输入的信息以及价格来自何处。

最后,在存储了所有信息之后,它会打印出有关口味和大小的正确选择,但最终每英寸的价格和总成本都为零。

public static void main(String[] args) {

        float biteSize = 3;                                                     
        float small = 9;
        float large = 12;
        String chosenSize = "";
        double pricePerInch = 0;
        double total = 0;
        String userFlavor = "";
        String[] chooseSizes = {"bite size", "small", "large"};
        String[] chooseFlavors = {"plain", "strawberry", "raspberry", "caramel", "chocolate"} ;

        Scanner scnr = new Scanner(System.in);

        System.out.println("Flavors to choose from: plain, strawberry, raspberry, caramel, chocolate.");        //giving user flavor list

        boolean flavorFound = false;

        while (!flavorFound) {                      

          System.out.println("Please choose flavor:");                                  

          userFlavor = scnr.nextLine();

          for(int i = 0; i < chooseFlavors.length; i++) {                               

              if(userFlavor.equals(chooseFlavors[i])) {                                 

                  System.out.println(userFlavor);               

                  flavorFound = true; 

                  break;

              } else {

                  System.out.println("Please choose from flavors above.");
              }
          }

        }

        if (userFlavor == chooseFlavors[0]) {                                           

            pricePerInch = 0.50;

        }
        else if (userFlavor == chooseFlavors[1]) {
            pricePerInch = 1.25;
        }
        else if (userFlavor == chooseFlavors[2]) {
            pricePerInch = 1.15;
        }
        else if (userFlavor == chooseFlavors[3]) {
            pricePerInch = 0.75;
        }
        else if (userFlavor == chooseFlavors[4]) {
            pricePerInch = 0.85;
        }

        System.out.println("Sizes to choose from: bite size, small, large");


        boolean sizeFound = false;

        while (!sizeFound) {            

          System.out.println("Please choose cheese cake size: ");   

          chosenSize = scnr.nextLine(); 

          for(int i = 0; i < chooseSizes.length; i++) {     

              if(chosenSize.equals(chooseSizes[i])) { 

                  System.out.println(chosenSize);

                  sizeFound = true;         

                  break;        

              } else {

                  System.out.println("Please choose from size above.");
              }
          }

        }

        if (chosenSize.contains("bite size") ) {                                            

            total = pricePerInch * biteSize;

        }
        else if (chosenSize.contains("small")) {

            total = pricePerInch * small;

        }
        else if (chosenSize.contains("large")) {

            total = pricePerInch * large;

        }

        System.out.println("Your chosen flavor:  " + userFlavor);                                   
        System.out.println("Your chosen size: " + chosenSize);

        System.out.println("Price for topping: " + "$" + pricePerInch + " per inch.");

        System.out.println("Size of cheesecake: " + chosenSize + " inches.");

        System.out.printf("Your total cost will be: $" +  total + " dollars.");
    }

}

0 个答案:

没有答案