繁体   English   中英

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

[英]C# Console Application project to compute the total price of a transaction

我无法从用户那里获取多个输入、计算它们并在屏幕上显示它们。 还有一个折扣场景卡选项。如果他们有场景卡,每件商品的底价应降低 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);

我没有太多更改您的代码,因为我假设您希望它尽可能基本。 刚刚添加了一个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);

    }

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

①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM