while 循环放置在哪里

问题描述 投票:0回答:1

我有一个程序可以处理用户要求不同纸杯蛋糕的订单。我遇到的问题是,一旦用户输入 0(表示他们希望完成订单),程序应该在用户输入 0 后立即继续运行,并使其进入下一个订单(#0002 )支付 0 钱,同时将剩余纸杯蛋糕的数量减少到前一个用户订购的数量。输出应该是这样的:enter image description here 一直到纸杯蛋糕用完,输出如下:enter image description here 我遇到的问题是,我的程序在完成第一个订单后立即停止,并且不再接受任何用户输入。我知道我应该使用 while 循环,它使用“stock”布尔值来控制它,但我不确定将它放在哪里。 这是我的代码:


import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

        public class TheCupcakeFactory 
        {
            private static Scanner inputFile;
            /*
             *Diego Molina Salinas
             *CS160 Fall 2023
             *Project 4: The Cupcake Factory Sales
             *
             */
            public static void main(String[] args) throws IOException
            {
                Cupcake cupcakeCS = new Cupcake();
                Cupcake cupcakeAP = new Cupcake();
                Cupcake cupcakeBF = new Cupcake();
                Cupcake cupcakeBS = new Cupcake();
                Cupcake cupcakeBW = new Cupcake();
                int quantity = 0;
                int orderNum = 0;
                boolean stock = true;
                Cupcake [] cupcakes = {cupcakeCS, cupcakeAP, cupcakeBF, cupcakeBS, cupcakeBW};
                System.out.println("Welcome to the Cupcake Factory Sales Registry!!");

                
                    FileReader file = new FileReader("FreshCupcakes.txt");
                    inputFile = new Scanner(file);
//                  while(stock) {
                    orderNum++;
                    printMenu(cupcakeCS, "Moist chocolate cake filled with strawberry jam, topped with strawberry buttercream dipped in chocolate.", 2.75);
                    printMenu(cupcakeAP, "Moist vanilla cake filled with apple pie filling; topped with cream cheese icing, apple pie filling, and cinnamon streusel", 3.25);
                    printMenu(cupcakeBF, "Moist chocolate cake filled and topped with our peanut butter icing and Butterfinger candy.", 2.95);
                    printMenu(cupcakeBS, "Moist chocolate cake filled with banana filling, topped with our vanilla buttercream with sprinkles, cherry and chocolate drizzle on top.", 3.15);
                    printMenu(cupcakeBW, "Moist vanilla cupcake, topped with our rich fudge icing", 3.50);
//                  if(cupcakeCS.getUnitsOnHand() == 0 && cupcakeAP.getUnitsOnHand() == 0 && cupcakeBF.getUnitsOnHand() == 0 && cupcakeBS.getUnitsOnHand() == 0 && cupcakeBW.getUnitsOnHand() == 0 )
//                  {
//                      System.out.println("We are sorry, our yummy cupcakes are sold out.");
//                      stock = false;
//                      break;
//                  }
//                  }
                    for (int i = 0; i < cupcakes.length; i++) 
                    {
                        System.out.println((i + 1) + ".) " + cupcakes[i]);
                    }
                    Scanner kb = new Scanner(System.in);
                    boolean loopRunner = true;
                    int [] quantities = new int[cupcakes.length];
                    while (loopRunner) 
                    {
                        System.out.println("Please enter your choice (0 if done): ");
                        int choice = kb.nextInt();

                        if (choice == 0 ) 
                        {
                            printReceipt(cupcakes, quantities, orderNum);
                            break;
                        }

//                      if(cupcakeCS.getUnitsOnHand() == 0 && cupcakeAP.getUnitsOnHand() == 0 && cupcakeBF.getUnitsOnHand() == 0 && cupcakeBS.getUnitsOnHand() == 0 && cupcakeBW.getUnitsOnHand() == 0 )
//                      {
//                          System.out.println("We are sorry, our yummy cupcakes are sold out.");
//                          loopRunner = false;
//                          break;
//                      }
                        if (choice < 1 || choice > cupcakes.length) 
                        {
                            System.out.println("Invalid choice. Pick again.");
                            continue;
                        }
                         quantity = 0;
                        boolean validQuantity = false;
                        while(!validQuantity)
                        {
                        System.out.println("How many pieces? ");
                        quantity = kb.nextInt();

                        if (quantity <= cupcakes[choice - 1].getUnitsOnHand()) 
                        {
                            validQuantity = true;
                        } 
                        else 
                        {
                            System.out.println("Not enough units available for that cupcake.");
                        }
                        }
                        quantities[choice - 1] = quantity;
                        double totalCost = costTotal(cupcakes[choice - 1].getPrice(), quantity);
                        System.out.println("Total Cost: $" + totalCost);
                        cupcakes[choice - 1].setUnitsOnHand(cupcakes[choice - 1].getUnitsOnHand() - quantity);

                        
            }
                    printReceipt(cupcakes, quantities, orderNum);

            }
            public static void printMenu(Cupcake cupcake, String description, double price) 
            {
                cupcake.setFlavor(inputFile.next());
                cupcake.setDescription(description);
                cupcake.setUnitsOnHand(inputFile.nextInt());
                cupcake.setPrice(price);
            }
             public static double costTotal(double price, int quantity) 
             {
                    return price * quantity;
             }
             public static void getTax(double grandTotal)
                {
                    double totalWithTax = grandTotal *1.07;
                    System.out.println("Here's your total (Including Tax:) " + totalWithTax);
                }
             public static void printReceipt(Cupcake[] cupcakes, int [] quantities, int orderNum) 
             {
                 
                    System.out.println("Order #000" + orderNum);

                    double grandTotal = 0;

                    for (int i = 0; i < quantities.length; i++) {
                        if(quantities[i] > 0)
                        {
                            double cupcakeTotal = quantities[i] * cupcakes[i].getPrice();
                            System.out.printf("%-25s $%.2f\n", cupcakes[i].getFlavor(), cupcakeTotal);
                            grandTotal +=cupcakeTotal;
                        }

                    }

                    System.out.println("\tTotal\t\t$" + grandTotal);
                    getTax(grandTotal);
             }
        }

如您所见,我尝试使用库存循环的多行已被注释掉。我把它们留在那里,以防我遇到问题,这样你们就可以让我知道如何解决这个问题。

这是我的输出:

Welcome to the Cupcake Factory Sales Registry!!
1.) Chocolate-Strawberry:  $2.75 - 
Moist chocolate cake filled with strawberry jam, topped with strawberry buttercream dipped in chocolate.
2.) Apple-Pie:  $3.25 - 
Moist vanilla cake filled with apple pie filling; topped with cream cheese icing, apple pie filling, and cinnamon streusel
3.) Butterfinger:  $2.95 - 
Moist chocolate cake filled and topped with our peanut butter icing and Butterfinger candy.
4.) Banana-Split:  $3.15 - 
Moist chocolate cake filled with banana filling, topped with our vanilla buttercream with sprinkles, cherry and chocolate drizzle on top.
5.) Black&White:  $3.5 - 
Moist vanilla cupcake, topped with our rich fudge icing
Please enter your choice (0 if done): 
3
How many pieces? 
2
Total Cost: $5.9
Please enter your choice (0 if done): 
0
Order #0001
Butterfinger              $5.90
    Total       $5.9
Here's your total (Including Tax:) 6.313000000000001
Order #0001
Butterfinger              $5.90
    Total       $5.9
Here's your total (Including Tax:) 6.313000000000001

这是计算机停止接受用户输入的地方。

尝试放置一个 while 循环,以便程序在输入第一个订单后继续运行,但不起作用,计算机在用户点击 0 的第一个订单后停止接受输入。

java while-loop file-io
1个回答
0
投票

你的代码必须看起来有点像这样:

while (have_stock) {
    Order order = getOrderInformation();
    processOrder(order);
}

现在,您在循环中放置的表达式非常灵活 - 我选择将大部分逻辑放入两种不同的方法中。如果您愿意,可以采用不同的方式。 您应该很快就能到达那里,因为听起来应用程序已经可以处理单个订单了。

© www.soinside.com 2019 - 2024. All rights reserved.