从总价中正确扣除折扣? [关闭]

问题描述 投票:0回答:1

我有这个代码,它接受一定数量的项目。每件商品的基本价格为6.00美元,然后根据商品数量给予折扣,1-4件商品没有折扣,5-9件有10%的折扣,10-14件有14%的折扣, 15或以上可获得20%的折扣。我运行该程序,但它似乎输出最终价格,而没有从总价格中扣除折扣。我究竟做错了什么?

 static void Main(string[] args)
    {
        int quantity;
        double price;
        quantity = GetQuantity();
        price = CalculatePrice(quantity);
        WriteLine("Final price for {0} items is {1}.",
          quantity, price.ToString("c"));

    }

    private static int GetQuantity()
    {
        int quantity;
        Write("Enter number of items >> ");
        quantity = Convert.ToInt32(ReadLine());
        return quantity;

    }
    private static double CalculatePrice(int quantityOrdered)
    {
        double PRICE_PER_ITEM = 6.00;
        double price = 0;
        double discount = 0;
        int[] quanLimits = { 0, 5, 10, 15 };
        double[] limits = { 0, 0.10, 0.14, 0.20 };
        for (int x = limits.Length - 1; x >= 0; x--)
            if (quantityOrdered >= quanLimits[x])
                discount = limits[x];
        //int x = 0;
        price = quantityOrdered * PRICE_PER_ITEM;
        price = price - (price * discount);
        return price;
    }
c# loops percentage price
1个回答
1
投票

你的for循环内部条件是错误的。它遍历所有项目,并且由于给定数量始终> = 0(最后一个查询是quanLimits数组中的第一个元素),最后一个赋值是discount = 0。这就是没有计算折扣的原因。您可以通过反转for循环来解决此问题,例如:从索引0开始。

© www.soinside.com 2019 - 2024. All rights reserved.