gpt4 book ai didi

java - 如何获得循环内每个商品的总销售成本?

转载 作者:行者123 更新时间:2023-12-01 14:49:38 25 4
gpt4 key购买 nike

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

最佳答案

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

关于java - 如何获得循环内每个商品的总销售成本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15017016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com