2013-02-22 58 views
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++; 
    } 
+0

[你試過什麼](http://www.whathaveyoutried)。 (提示:你可能需要幾個計數器。) – 2013-02-22 04:49:19

+0

我在while循環中嘗試了一條for語句,但是沒有成功,因爲我不知道如何將成本加在一起,而不是客戶買了多少次項目。 – Pandokie 2013-02-22 04:54:25

+0

請發佈代碼,以便我們能夠爲您提供幫助。例如,如果您知道客戶購買了5杯拿鐵,那麼拿鐵的成本是多少?你是怎麼計算的? – 2013-02-22 04:55:19

回答

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++; 
    } 
} 

基本上,當在初始化和環路內定義一個變量,雖然循環之外,它+=,它可以在循環外訪問和將「記住」整個代碼是RAN;如果這對你有意義。

+0

我明白了。謝謝。這看起來比我想要適應的累加器容易得多。 – Pandokie 2013-02-22 15:00:50

0

您可以爲每個項目創建一個全局變量,並隨時在這些if-else語句內選擇它時增加該值。這樣,不管隨機選擇什麼,全局變量都會增加。您可以稍後根據價格將其總計。我希望這就是你要找的。

+0

變量不需要是「全局」(或更準確地說是成員變量或類變量),如果它們只用於與OP發佈的方法相同的方法,它們可以很容易成爲局部變量。 – 2013-02-22 04:57:21

+0

你是絕對正確的。謝謝你指出。 DERP! – Bibodha 2013-02-23 00:42:31

1

我能想到的唯一方法是創建四個不同的計數器,並通過每次添加它來存儲每次銷售。

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不是靜態方法。