C#if else没有执行

时间:2016-07-17 09:07:55

标签: c# if-statement

enter image description here

if else语句不执行......这是代码:

这应该根据付款日期来计算折扣和购买项目的总金额

        DateTime dPurchase, dPaid;
        int amount;
        double discount, totalAmount, days;

        S.Write("Purchase amount: ");
        amount = Convert.ToInt16(S.ReadLine());
        S.Write("Date Purchased: ");
        dPurchase = Convert.ToDateTime(S.ReadLine());
        S.Write("Date to be Paid: ");
        dPaid = Convert.ToDateTime(S.ReadLine());

        days = dPaid.Subtract(dPurchase).TotalDays;

        if (days <= 10) 
        {
            discount = amount * 0.2;
            totalAmount = amount - discount;
            S.Write("Discount: " + discount + "\nTotal Amount: " + totalAmount);
        }
        else if (days <= 25)
        {
            discount = amount * 0.15;
            totalAmount = amount - discount;
            S.Write("Discount: " + discount + "\nTotal Amount: " + totalAmount);
        }
        else if (days <= 35)
        {
            discount = amount * 0.1;
            totalAmount = amount - discount;
            S.Write("Discount: " + discount + "\nTotal Amount: " + totalAmount);
        }
        else if (days <= 45)
        {
            discount = amount * 0.05;
            totalAmount = amount - discount;
            S.Write("Discount: " + discount + "\nTotal Amount: " + totalAmount);
        }
        else 
        {
            discount = 0;
            totalAmount = amount;
        }
        S.Write("Days: " + days + "\n");
        S.Write("First Term: " + dPurchase.Date.AddDays(1) + " to " + dPurchase.Date.AddDays(10) + "\n");
        S.Write("Second Term: " + dPurchase.Date.AddDays(11) + " to " + dPurchase.Date.AddDays(25) + "\n");
        S.Write("Third Term: " + dPurchase.Date.AddDays(26) + " to " + dPurchase.Date.AddDays(35) + "\n");
        S.Write("Fourth Term: " + dPurchase.Date.AddDays(36) + " to " + dPurchase.Date.AddDays(45) + "\n");
        S.Write("Fifth Term: " + dPurchase.Date.AddDays(46) + " to " + dPurchase.Date.AddDays(60) + "\n");
        S.ReadKey();
执行if..else语句后

4 个答案:

答案 0 :(得分:4)

如果您遵循DRY规则

  

D R epeat Y oursef

你不会遇到这种问题

DateTime dPurchase, dPaid;
int amount;
double? discount = null;
double totalAmount, days;

S.Write("Purchase amount: ");
amount = Convert.ToInt16(S.ReadLine());
S.Write("Date Purchased: ");
dPurchase = Convert.ToDateTime(S.ReadLine());
S.Write("Date to be Paid: ");
dPaid = Convert.ToDateTime(S.ReadLine());

days = dPaid.Subtract(dPurchase).TotalDays;

if (days <= 10) 
    discount = amount * 0.2;
else if (days <= 25)
    discount = amount * 0.15;
else if (days <= 35)
    discount = amount * 0.1;
else if (days <= 45)
    discount = amount * 0.05;

totalAmount = amount - discount.GetValueOrDefault();    

if (discount.HasValue)
  S.Write("Discount: " + discount.Value );

S.Write( "Total Amount: " + totalAmount);

S.Write("Days: " + days + "\n");
S.Write("First Term: " + dPurchase.Date.AddDays(1) + " to " + dPurchase.Date.AddDays(10) + "\n");
S.Write("Second Term: " + dPurchase.Date.AddDays(11) + " to " + dPurchase.Date.AddDays(25) + "\n");
S.Write("Third Term: " + dPurchase.Date.AddDays(26) + " to " + dPurchase.Date.AddDays(35) + "\n");
S.Write("Fourth Term: " + dPurchase.Date.AddDays(36) + " to " + dPurchase.Date.AddDays(45) + "\n");
S.Write("Fifth Term: " + dPurchase.Date.AddDays(46) + " to " + dPurchase.Date.AddDays(60) + "\n");
S.ReadKey();

正如您所看到的,它具有更少的代码,更易于阅读/维护,并且更不容易出错。

答案 1 :(得分:1)

这是因为days值大于45.您是否尝试手动将days变量设置为小于45的值?

这对我来说很好。

答案 2 :(得分:1)

您希望执行第S.Write("Discount: " + discount + "\nTotal Amount: " + totalAmount);行。在您的示例中,days等于`46。

只执行最后一个else块,因此行S.Write(...)不是。

为什么?

  • 第一个if-block只在days小于或等于10时执行.46大于10,所以跳过它。
  • 仅当days大于10且小于时才执行第二个if块
    1. 46大于25,因此被跳过。
  • 仅当days大于25且小于35时才执行第三个if块.46大于35,因此跳过它。
  • 第四个if-block仅在days大于35且小于45时执行.46大于25,因此跳过。

答案 3 :(得分:0)

我认为你打算在最后一个之后放置这一行:

else
{
    discount = 0;
    totalAmount = amount;
}
Console.WriteLine("Discount: " + discount + "\nTotal Amount: " + totalAmount);

并将其从块中删除。这样它就可以在所有情况下显示出来。