이 루프 중에는 선택할 수있는 항목이 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). (힌트 : 아마도 여러 개의 카운터가 필요할 것입니다.) –
while 루프 내에서 for 문을 시도했지만 고객이 특정 횟수만큼 구매 한 횟수 대신 비용을 함께 추가하는 방법을 모르기 때문에 문제가 해결되지 않았습니다. 목. – Pandokie
우리가 당신을 도울 수 있도록 코드를 게시하십시오. 예를 들어 고객이 5 라떼를 구입했다는 것을 알고 있다면 라떼 비용은 얼마입니까? 어떻게 계산 했니? –