.net 用于计算交易总价的C#控制台应用程序项目

z3yyvxxp  于 2023-02-06  发布在  .NET
关注(0)|答案(1)|浏览(147)

我不能从用户那里获得多个输入,计算它们,并在屏幕上显示它们。还有一个场景卡选项的折扣。如果他们有场景卡与他们,基本价格应减少10%的每一个项目。(即苹果将成本1.575美元每磅和番茄是苹果相同)。每个牛奶袋将成本6.00美元,如果他们有场景卡。

Console.WriteLine("enter how many items you purchased");
        int numberOfItems = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the name of item purchased: ");
        string[] strings= new string[numberOfItems];    
       

        Console.WriteLine("Enter the name of item purchased: ");
        string itemName = Console.ReadLine();
        double weight;
        int numberOfBags;
        double basePrice = 0;
        bool hasScenaCard = false;

        switch (itemName.ToLower())
        {
            case "apples":
                basePrice = 1.75;
                break;
            case "chillies":
                basePrice = 1.29;
                break;
            case "tomatoes":
                basePrice = 1.45;
                break;
            case "milk":
                basePrice = 6.54;
                break;
            default:
                Console.WriteLine("Invalid item name entered.");
                return;
        }

        Console.WriteLine("Enter the weight of the item purchased (in lbs): ");
        weight = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("Enter the number of store bags used: ");
        numberOfBags = Convert.ToInt32(Console.ReadLine());


        Console.WriteLine("Do you have a Scena card (yes/no): ");
        hasScenaCard = Console.ReadLine().ToLower() == "yes";
        if (itemName.ToLower() != "milk")
        {
            basePrice *= weight;
            if (hasScenaCard)
            {
                basePrice *= 0.10;
            }
        }
        else
        {
            if (hasScenaCard)
            {
                basePrice = 6.00;
            }
            basePrice *= weight;
        }
        double storeBagsCost = numberOfBags * 0.5;
        double subtotal = basePrice + storeBagsCost;
        double hstAmount = subtotal * 0.13;
        double grandTotal = subtotal + hstAmount;
        int scenePoints = (int)(weight * 20);

        Console.WriteLine("Item Name: " + itemName);
        Console.WriteLine("Base Price: $" + basePrice);
        Console.WriteLine("Store Bags Cost: $" + storeBagsCost);
        Console.WriteLine("Subtotal: $" + subtotal);
        Console.WriteLine("HST Amount: $" + hstAmount);
        Console.WriteLine("Grand Total: $" + grandTotal);
        Console.WriteLine("Total Scene Points Earned: " + scenePoints);
yhxst69z

yhxst69z1#

我没有对代码做太多修改,因为我认为您希望它尽可能简单,只是添加了一个for循环,以便根据购买的物品数量从用户那里获得多个输入。

public static void GetLetters()
    {
        Console.WriteLine("enter how many items you purchased");
        int numberOfItems = int.Parse(Console.ReadLine());
        //Console.WriteLine("Enter the name of item purchased: ");
        //string[] strings = new string[numberOfItems]; // I'm not sure what are these lines doing

        double weight = 0;
        int numberOfBags = 0;
        double basePrice = 0.0;
        bool hasScenaCard = false;
        string itemName = string.Empty;

        for (int i = 0; i < numberOfItems; ++i)
        {
            Console.WriteLine("Enter the name of item purchased: ");
            itemName = Console.ReadLine();

            switch (itemName.ToLower())
            {
                case "apples":
                    basePrice = 1.75;
                    break;
                case "chillies":
                    basePrice = 1.29;
                    break;
                case "tomatoes":
                    basePrice = 1.45;
                    break;
                case "milk":
                    basePrice = 6.54;
                    break;
                default:
                    Console.WriteLine("Invalid item name entered.");
                    return;
            }

            Console.WriteLine("Enter the weight of the item purchased (in lbs): ");
            weight = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter the number of store bags used: ");
            numberOfBags = Convert.ToInt32(Console.ReadLine());


            Console.WriteLine("Do you have a Scena card (yes/no): ");
            hasScenaCard = Console.ReadLine().ToLower() == "yes";
            if (itemName.ToLower() != "milk")
            {
                basePrice *= weight;
                if (hasScenaCard)
                {
                    basePrice *= 0.10;
                }
            }
            else
            {
                if (hasScenaCard)
                {
                    basePrice = 6.00;
                }
                basePrice *= weight;
            }
        }

        double storeBagsCost = numberOfBags * 0.5;
        double subtotal = basePrice + storeBagsCost;
        double hstAmount = subtotal * 0.13;
        double grandTotal = subtotal + hstAmount;
        int scenePoints = (int)(weight * 20);

        Console.WriteLine("Item Name: " + itemName);
        Console.WriteLine("Base Price: $" + basePrice);
        Console.WriteLine("Store Bags Cost: $" + storeBagsCost);
        Console.WriteLine("Subtotal: $" + subtotal);
        Console.WriteLine("HST Amount: $" + hstAmount);
        Console.WriteLine("Grand Total: $" + grandTotal);
        Console.WriteLine("Total Scene Points Earned: " + scenePoints);

    }

注意需要进行大量验证,以确保您从用户那里获得正确类型的输入。

相关问题