如何获得循环内每个项目的总销售成本?

时间:2013-02-22 04:45:48

标签: java if-statement while-loop sum random-sample

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

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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

答案 2 :(得分: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(...)

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

答案 3 :(得分: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循环之外初始化和定义一个变量时,+=它在循环中,它可以在循环外部访问,并且在整个运行的代码中都会被“记住”;如果这对你有意义的话。