为了账号安全,请及时绑定邮箱和手机立即绑定

从总价中正确扣除折扣?

从总价中正确扣除折扣?

C#
侃侃尔雅 2022-12-24 10:53:50
我有这段代码,可以为一定数量的物品输入一个数字。每件商品的底价为 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;    }
查看完整描述

1 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

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



查看完整回答
反对 回复 2022-12-24
  • 1 回答
  • 0 关注
  • 41 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信