简体   繁体   English

使用C#进行分配以应用折扣。 我正在使用if-else

[英]Assignment with C# to apply discount. I'm using if-else

My assignment is: 我的任务是:

Each item sold is $99. 每件售出价格为$ 99。

You get no discount selling under 10, so 1-9. 低于10,您没有折扣,所以1-9。

You get a 5% discount selling 10-19. 您会以5%的折扣出售10-19。

You get a 10% discount selling 20+. 您可以通过销售20+获得10%的折扣。

When I put 1-9 numbers in my textbox, it doesn't apply the discount which is good. 当我在文本框中输入1-9个数字时,它不会应用折扣,这是很好的。 When I put 10-19, it applies the 5% off discount. 当我放10-19时,它适用5%的折扣。 But when I put 20 or any numbers higher, it still applies the 5% off discount and not a 10% discount. 但是,当我输入20或更高的数字时,它仍会应用5%的折扣,而不是10%的折扣。

My code: 我的代码:

double dblQuantitySold;
double dblAmountTotalDue;
double dblPrice = 99;

//Acquire the math.
dblQuantitySold = double.Parse(txtQuantity.Text);
dblAmountTotalDue = dblQuantitySold * dblPrice;

//Create the solution for your if-else statement.
if (dblQuantitySold >= 10)
{
    dblAmountTotalDue -= dblQuantitySold * dblPrice * 0.05;
    MessageBox.Show("A 5% discount will be given because 10 or more has been sold.");
}
else if (dblQuantitySold >= 20)
{
    dblAmountTotalDue -= dblQuantitySold * dblPrice * 0.10;
    MessageBox.Show("A 10% discount will be given because 20 or more has been sold.");
}

//Display the results.
lblAmountDueTotal.Text = dblAmountTotalDue.ToString("C");

Do this instead. 改为这样做。 Add additional conditional operator on your first if which will react as between two number condition. 如果将在两个数字条件之间做出反应,则在第一个条件上添加其他条件运算符。

if (dblQuantitySold >= 10 && dblQuantitySold < 20)
{
    dblAmountTotalDue -= dblQuantitySold * dblPrice * 0.05;
    MessageBox.Show("A 5% discount will be given because 10 or more has been sold.");
}

It's because you have the if statements in the wrong order. 这是因为您的if语句顺序错误。 You first check if the item is greater than or equal to 10, which includes all values greater than 20. Just put the check for >=20 first: 您首先检查项目是否大于或等于10,其中包括所有大于20的值。只需将>=20放在第一位即可:

if (dblQuantitySold >= 20)
{
    dblAmountTotalDue -= dblQuantitySold * dblPrice * 0.10;
    MessageBox.Show("A 10% discount will be given because 20 or more has been sold.");
}
else if (dblQuantitySold >= 10)
{
    dblAmountTotalDue -= dblQuantitySold * dblPrice * 0.05;
    MessageBox.Show("A 5% discount will be given because 10 or more has been sold.");
}

Try this: 尝试这个:

if (dblQuantitySold >= 20)
{
    dblAmountTotalDue -= dblQuantitySold * dblPrice * 0.10;
    MessageBox.Show("A 10% discount will be given because 20 or more has been sold.");
}
else if (dblQuantitySold >= 10)
{
    dblAmountTotalDue -= dblQuantitySold * dblPrice * 0.05;
    MessageBox.Show("A 5% discount will be given because 10 or more has been sold.");
}

Your conditions were in the wrong order. 您的条件顺序错误。

If you want to go for bonus points try this code: 如果您想获得奖励积分,请尝试以下代码:

decimal price = 99;

decimal quantitySold = decimal.Parse(txtQuantity.Text);

var discounts = new[]
{
    new { percentage = 0.0m, threshold = 0 },
    new { percentage = 0.05m, threshold = 10 },
    new { percentage = 0.1m, threshold = 20 },
};

var discount =
    discounts
        .OrderBy(d => d.threshold)
        .Where(d => d.threshold <= quantitySold)
        .Last();

decimal amountTotalDue = quantitySold * price * (1m - discount.percentage);

if (discount.percentage > 0m)
{
    var message = String.Format(
        "A {0:0%}% discount will be given because {1} or more has been sold.",
        discount.percentage,
        discount.threshold)

    MessageBox.Show(message);
}

lblAmountDueTotal.Text = amountTotalDue.ToString("C");

The advantage here is that you separate the discount structure from the code that computes the discount. 这样做的好处是,您可以将折扣结构与计算折扣的代码分开。 This allows you to then extract away the discounts into a file or database and allow the application to be easily customizable. 然后,您可以将折扣提取到文件或数据库中,并可以轻松地自定义应用程序。

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

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