我的 C# 订单程序计算不正确

本文关键字:程序 计算 不正确 单程序 我的 | 更新日期: 2023-09-27 18:37:01

我需要一些关于我的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!!");
    }
}

}

我的 C# 订单程序计算不正确

任务结束时的计算是错误的。您使用了错误的变量

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);

最后,在进行涉及货币值的计算时,应使用十进制数据类型。