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

私が考えることができる唯一の方法は、4つの異なるカウンターを作成し、毎回追加して各販売を保存することです。

于 2013-02-22T04:54:14.777 に答える
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 に答える
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(...)

formatが static メソッドではないため、コンパイルされません。

于 2013-02-22T04:56:09.807 に答える