我的C#程序需要一些帮助。它应该询问在聚会中有多少人,然后用户输入他们的主菜价格,如果他们想要一杯饮料添加,然后打折。我遇到的问题是,如果我在聚会中不止一个并且他们都得到折扣,那么就不能给出正确的结果。这是我的代码。
class Program
{
static void Main(string[] args)
{
int inParty = 0;
int discoutSelection = 0;
double entreeAmount = 0;
int drinkSelection = 0;
double drinkPrice = 1.25;
double discount = 0;
double orderAmount = 0;
double tax = 0.07;
double taxAmount = 0;
double finalTotal = 0;
Console.WriteLine("**********Welcome to K&W**********\n\n");
Console.Write("How many are in your part? ");
inParty = Convert.ToInt32(Console.ReadLine());
while(inParty > 0)
{
inParty--;
Console.Write("\nEnter your entree amount: $");
entreeAmount = Convert.ToDouble(Console.ReadLine());
Convert.ToDouble(orderAmount += entreeAmount);
Console.Write("Do you want a drink, enter 1 for yes or another number for no. ");
drinkSelection = Convert.ToInt32(Console.ReadLine());
if (drinkSelection == 1)
{
Convert.ToDouble(orderAmount += drinkPrice);
}
else
{
Console.WriteLine("\tNo drink at this time");
}
Console.Write("\nPlease choose a discount, enter 1 for senior, 2 for child or 3 for none: ");
discoutSelection = Convert.ToInt32(Console.ReadLine());
if (discoutSelection == 1)
{
//Convert.ToDouble(discount = 0.10);
orderAmount = Convert.ToDouble(orderAmount * 0.90);
}
if (discoutSelection == 2)
{
//Convert.ToDouble(discount = 0.20);
orderAmount = Convert.ToDouble(orderAmount * 0.80);
}
finalTotal += Convert.ToDouble(orderAmount);
entreeAmount = 0;
drinkSelection = 0;
discoutSelection = 0;
orderAmount = 0;
}
taxAmount = Convert.ToDouble(orderAmount * tax);
finalTotal = Convert.ToDouble(orderAmount + taxAmount);
Console.WriteLine("\nYour bill is below:\n");
Console.WriteLine("Your subtotal is\t{0:C}", orderAmount);
Console.WriteLine("Tax amount is\t{0:C}", taxAmount);
Console.WriteLine("Your final bill is:\t{0:C}", finalTotal);
Console.WriteLine("\n\nThank you for your order come again soom!!");
}
}
}
答案 0 :(得分:1)
您在任务结束时的计算是错误的。您使用的是错误的变量
double subTotal = finalAmount;
taxAmount = Convert.ToDouble(finalTotal * tax);
finalTotal = Convert.ToDouble(finalTotal + taxAmount);
orderAmount在每个循环结束时重置为零
Console.WriteLine("\nYour bill is below:\n");
Console.WriteLine("Your subtotal is\t{0:C}", subTotal);
Console.WriteLine("Tax amount is\t{0:C}", taxAmount);
Console.WriteLine("Your final bill is:\t{0:C}", finalTotal);
最后,在进行涉及货币值的计算时,应使用decimal数据类型。