Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用整数编写switch语句_C#_Switch Statement - Fatal编程技术网

C# 使用整数编写switch语句

C# 使用整数编写switch语句,c#,switch-statement,C#,Switch Statement,我需要使用switch语句来编写此信息,但似乎无法理解如何使用switch做不到您想要的事情,但您可以将其简化为: if (intDaysOverdue <= 30) { decInterestRate = 0m; } else if (intDaysOverdue >= 30 && intDaysOverdue <= 59) { decInterestRate = .5m; } else if (intDaysO

我需要使用switch语句来编写此信息,但似乎无法理解如何使用switch做不到您想要的事情,但您可以将其简化为:

if (intDaysOverdue <= 30)
{
            decInterestRate = 0m;
}
else if (intDaysOverdue >= 30 && intDaysOverdue <= 59)
{
            decInterestRate = .5m;
}
else if (intDaysOverdue >= 60 && intDaysOverdue <= 89)
{
            decInterestRate = .10m;
}
else if (intDaysOverdue >= 90)
{
            decInterestRate = .15m;
} 

您不能在这里使用switch,您可以使用默认情况操纵它,但为什么要这样做呢?
switch和case用于测试单个值,您的条件需要一个if语句。

我想不出在这里使用switch-case的方法。您尝试了什么?另外,您确定需要将其作为switch语句编写吗?您不能为此使用switch。为什么需要这样做?另外,前面的条件语句暗示了所有>=比较都是多余的。@EZI,这是一个很好的解决方法,虽然是针对OP的,但没有理由过分复杂,并且通过执行建议的操作来更难理解这一点,只要使用if条件,这就是它们的用途。谢谢。我想起来了out@user4338480没问题,如果这有帮助,请勾选旁边的复选标记,将其标记为已接受的答案。是否介意评论?
if (intDaysOverdue <= 30)
    decInterestRate = 0m;
else if (intDaysOverdue <= 59)
    decInterestRate = .5m;
else if (intDaysOverdue <= 89)
    decInterestRate = .10m;
else 
    decInterestRate = .15m;
decInterestRate = ((int)Math.Min(intDaysOverdue, 90) /30) * .5;