【发布时间】:2019-09-11 00:20:17
【问题描述】:
我有这个代码,它包含一个数量的项目。每件商品的底价为 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