0

在这个 while 循环中,可以选择 4 个项目。咖啡 (1)、拿铁 (2)、卡布奇诺 (3) 和浓缩咖啡 (4)。模拟选择一堆随机数填写每个客户的订单和数量,然后计算每个客户的总成本。现在我必须在这个while循环之后制作一个销售报告,我必须计算每个项目的总销售额。我该怎么做,因为数字是随机的,并且循环会根据客户数量自行重复?

while (CustomersSimulated <= MaxCustomersSimulated)
        {   
                    //customer number
        System.out.println("Customer " + CustomersSimulated);    


                //one random item for each customer     
    Random RandomList = new Random();
    int RandomItem = RandomList.nextInt(4) + 1;   

    if (RandomItem == 1)
        {
        System.out.println("Item purchased: Coffee");
        final double CoffeePrice = 1.50;

                          //random quantity for the item        
        Random RandomListTwo = new Random();    
        int RandomQuantity = RandomListTwo.nextInt(5) + 1;
        System.out.println("Quantity purchased: " + RandomQuantity);

                          //total cost for the one customer     
        double TotalCoffeeCost = RandomQuantity * CoffeePrice;  
        System.out.println("Total Cost: $" + NumberFormat.format(TotalCoffeeCost)); 
        }
    else if (RandomItem == 2)
        {
        System.out.println("Item purchased: Latte");
        final double LattePrice = 3.50;

        Random RandomListTwo = new Random();
        int RandomQuantity = RandomListTwo.nextInt(5) + 1;
        System.out.println("Quantity purchased: " + RandomQuantity);

        double TotalLatteCost = RandomQuantity * LattePrice; 
        System.out.println("Total Cost: $" + NumberFormat.format(TotalLatteCost));
        }
    else if (RandomItem == 3)
        {
        System.out.println("Item purchased: Cappuccino");
        final double CappuccinoPrice = 3.25;

        Random RandomListTwo = new Random();
        int RandomQuantity = RandomListTwo.nextInt(5) + 1;
        System.out.println("Quantity purchased: " + RandomQuantity);

        double TotalCappuccinoCost = RandomQuantity * CappuccinoPrice; 
        System.out.println("Total Cost: $" + NumberFormat.format(TotalCappuccinoCost));
        }
    else if (RandomItem == 4)
        {
        System.out.println("Item purchased: Espresso");
        final double EspressoPrice = 2.00;

        Random RandomListTwo = new Random();
        int RandomQuantity = RandomListTwo.nextInt(5) + 1;
        System.out.println("Quantity purchased: " + RandomQuantity);

        double TotalEspressoCost = RandomQuantity * EspressoPrice; 
        System.out.println("Total Cost: $" + NumberFormat.format(TotalEspressoCost));
        }

    System.out.println(" ");

    CustomersSimulated++;
    }
4

4 回答 4

1

我能想到的唯一方法是创建四个不同的柜台并通过每次添加来存储每次销售。

于 2013-02-22T04:54:14.777 回答
0

您可以为每个项目创建一个全局变量,并在这些 if-else 语句中选择它时增加值。这样,无论随机选择什么,全局变量都会递增。您可以稍后根据价格将其全部汇总。我希望这就是你要找的。

于 2013-02-22T04:53:21.827 回答
0

就像另一个说的那样,在循环之外创建一个新的计数器变量来存储总数。

看到这个:

        double TotalAllCoffeeCost = 0;
        double TotalAllLatteCost = 0;
        double TotalAllCappuccinoCost = 0;
        double TotalAllEspressoCost = 0;
        while (CustomersSimulated <= MaxCustomersSimulated) {
            // customer number
            System.out.println("Customer " + CustomersSimulated);

            // one random item for each customer
            Random RandomList = new Random();
            int RandomItem = RandomList.nextInt(4) + 1;

            if (RandomItem == 1) {
                System.out.println("Item purchased: Coffee");
                final double CoffeePrice = 1.50;

                // random quantity for the item
                Random RandomListTwo = new Random();
                int RandomQuantity = RandomListTwo.nextInt(5) + 1;
                System.out.println("Quantity purchased: " + RandomQuantity);

                // total cost for the one customer
                double TotalCoffeeCost = RandomQuantity * CoffeePrice;
                TotalAllCoffeeCost+=TotalCoffeeCost;
                System.out.println("Total Cost: $"
                        + new DecimalFormat().format(TotalCoffeeCost));
            } else if (RandomItem == 2) {
                System.out.println("Item purchased: Latte");
                final double LattePrice = 3.50;

                Random RandomListTwo = new Random();
                int RandomQuantity = RandomListTwo.nextInt(5) + 1;
                System.out.println("Quantity purchased: " + RandomQuantity);

                double TotalLatteCost = RandomQuantity * LattePrice;
                TotalAllLatteCost+=TotalLatteCost;
                System.out.println("Total Cost: $"
                        + new DecimalFormat().format(TotalLatteCost));
            } else if (RandomItem == 3) {
                System.out.println("Item purchased: Cappuccino");
                final double CappuccinoPrice = 3.25;

                Random RandomListTwo = new Random();
                int RandomQuantity = RandomListTwo.nextInt(5) + 1;
                System.out.println("Quantity purchased: " + RandomQuantity);

                double TotalCappuccinoCost = RandomQuantity * CappuccinoPrice;
                TotalAllCappuccinoCost+=TotalCappuccinoCost;
                System.out.println("Total Cost: $"
                        + new DecimalFormat().format(TotalCappuccinoCost));
            } else if (RandomItem == 4) {
                System.out.println("Item purchased: Espresso");
                final double EspressoPrice = 2.00;

                Random RandomListTwo = new Random();
                int RandomQuantity = RandomListTwo.nextInt(5) + 1;
                System.out.println("Quantity purchased: " + RandomQuantity);

                double TotalEspressoCost = RandomQuantity * EspressoPrice;
                TotalAllEspressoCost+=TotalEspressoCost;
                System.out.println("Total Cost: $"
                        + new DecimalFormat().format(TotalEspressoCost));
            }

            System.out.println(" ");

            System.out.println("Total Cofee Cost : " + new DecimalFormat().format(TotalAllCoffeeCost));
            System.out.println("Total Latte Cost : " + new DecimalFormat().format(TotalAllLatteCost));
            System.out.println("Total Cappucino Cost : " + new DecimalFormat().format(TotalAllCappuccinoCost));
            System.out.println("Total Espresso Cost : " + new DecimalFormat().format(TotalAllEspressoCost));

            CustomersSimulated++;
        }

无论如何,您的代码:

NumberFormat.format(...)

未编译,因为格式不是静态方法。

于 2013-02-22T04:56:09.807 回答
0

这是我将如何回答它。就像@IswantoSan 说的那样,NumberFormat.format()不编译所以我改变了它。此外,我摆脱了每次循环时发生的所有重新初始化。这是不需要的。不会占用太多内存,但我总是被告知这是一种很好的做法。此外,变量名中的第一个字母应小写。再一次,我总是被告知这是一种很好的做法。我可能是错的。

我希望这就是你要找的。

final static double espressoPrice = 2.00;
final static double cappuccinoPrice = 3.25;
final static double lattePrice = 3.50;
final static double coffeePrice = 1.50;

public static void main(String[] args) {

    int customersSimulated = 0;
    int maxCustomersSimulated = 4;
    int randomQuantity = 0;
    double total = 0;

    double totalCoffee = 0;
    double totalCappuccino = 0;
    double totalLatte = 0;
    double totalEspresso = 0;

    DecimalFormat df = new DecimalFormat("##.##");

    while (customersSimulated <= maxCustomersSimulated) { 

        System.out.println("Customer " + customersSimulated);    

        Random randomList = new Random();
        int randomItem = randomList.nextInt(4) + 1;   

        if (randomItem == 1) {

            System.out.println("Item purchased: Coffee");
            randomQuantity = randomList.nextInt(5) + 1;
            System.out.println("Quantity purchased: " + randomQuantity);
            double totalCoffeeCost = randomQuantity * coffeePrice;
            System.out.println("Total Cost: $" + df.format(totalCoffeeCost)); 

            totalCoffee+=totalCoffeeCost;
        } else if (randomItem == 2) {

            System.out.println("Item purchased: Latte");
            randomQuantity = randomList.nextInt(5) + 1;
            System.out.println("Quantity purchased: " + randomQuantity);
            double totalLatteCost = randomQuantity * lattePrice; 
            System.out.println("Total Cost: $" + df.format(totalLatteCost));

            totalLatte+=totalLatteCost;
        } else if (randomItem == 3) {

            System.out.println("Item purchased: Cappuccino");
            randomQuantity = randomList.nextInt(5) + 1;
            System.out.println("Quantity purchased: " + randomQuantity);
            double totalCappuccinoCost = randomQuantity * cappuccinoPrice; 
            System.out.println("Total Cost: $" + df.format(totalCappuccinoCost));

            totalCappuccino+=totalCappuccinoCost;
        } else if (randomItem == 4) {

            System.out.println("Item purchased: Espresso");
            randomQuantity = randomList.nextInt(5) + 1;
            System.out.println("Quantity purchased: " + randomQuantity);
            double totalEspressoCost = randomQuantity * espressoPrice; 
            System.out.println("Total Cost: $" + df.format(totalEspressoCost));

            totalEspresso+=totalEspressoCost;
        }

        System.out.println();
        System.out.println("Total Coffee Cost: $" + totalCoffee);
        System.out.println("Total Cappuccino Cost: $" + totalCappuccino);
        System.out.println("Total Llatte Cost: $" + totalLatte);
        System.out.println("Total Espresso Cost: $" + totalEspresso);
        System.out.println();

        customersSimulated++;
    }
}

基本上,当您在 while 循环之外初始化和定义一个变量时,+=它在循环内,它可以在循环外访问,并且将在整个运行的代码中被“记住”;如果这对你有意义。

于 2013-02-22T05:24:19.813 回答