在此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++;
}
[你試過什麼](http://www.whathaveyoutried)。 (提示:你可能需要幾個計數器。) – 2013-02-22 04:49:19
我在while循環中嘗試了一條for語句,但是沒有成功,因爲我不知道如何將成本加在一起,而不是客戶買了多少次項目。 – Pandokie 2013-02-22 04:54:25
請發佈代碼,以便我們能夠爲您提供幫助。例如,如果您知道客戶購買了5杯拿鐵,那麼拿鐵的成本是多少?你是怎麼計算的? – 2013-02-22 04:55:19