我怎样才能让这个方程在 C# 的嵌套 if/else 语句中工作?
当我在用户端获得 pass 20 后,我的 if/else 不会获取方程的下一个变体。我知道它是因为设置了总计 = (numberofchecks *二十检查) + (fee);但我不知道用什么来让它移动到方程的下一个变体。负数、超过 1000 和格式例外有效。
总计 = (支票数量 * 二十张支票) + (费用);
TotalLabel.Text = total.ToString("C");
// if else (5 or 6)
if ( numberofchecks < 20 && numberofchecks == 0)
{
total = (numberofchecks * twentychecks) + (fee);
}
else if ( numberofchecks < -1)
{
MessageBox.Show("Number of checks must be between 0 and 1000");
}
else if (numberofchecks == 20 && numberofchecks <= 39)
{
total = (numberofchecks * thirtyninechecks) + (fee);
}
else if (numberofchecks == 40 && numberofchecks <= 59)
{
total = (numberofchecks * fiftyninechecks) + (fee);
}
else if (numberofchecks == 60 && numberofchecks <=1000)
{
total = (numberofchecks * thousandchecks) + (fee);
}
else if (numberofchecks > 1000)
{
MessageBox.Show("Number of checks must be between 0 and 1000");
}
}
catch(FormatException )
{
MessageBox.Show(" Please enter a number");
}
After i get pass 20 on the user end my if/else doesn't pick up the next variation of the equatuion. I know its because of setting the total = (numberofchecks * twentychecks) + (fee); but i can't figure out what to use to get it to move to the next variation of the equation. The negative, over 1000, and format exceptions work.
total = (numberofchecks * twentychecks) + (fee);
TotalLabel.Text = total.ToString("C");
// if else (5 or 6)
if ( numberofchecks < 20 && numberofchecks == 0)
{
total = (numberofchecks * twentychecks) + (fee);
}
else if ( numberofchecks < -1)
{
MessageBox.Show("Number of checks must be between 0 and 1000");
}
else if (numberofchecks == 20 && numberofchecks <= 39)
{
total = (numberofchecks * thirtyninechecks) + (fee);
}
else if (numberofchecks == 40 && numberofchecks <= 59)
{
total = (numberofchecks * fiftyninechecks) + (fee);
}
else if (numberofchecks == 60 && numberofchecks <=1000)
{
total = (numberofchecks * thousandchecks) + (fee);
}
else if (numberofchecks > 1000)
{
MessageBox.Show("Number of checks must be between 0 and 1000");
}
}
catch(FormatException )
{
MessageBox.Show(" Please enter a number");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我们只查看您的IF语句之一
,如果
numberFchecks
等于20,那么我们会看到以下良好的状态,然后始终是&lt; = 39,因此第二部分是多余的。所有陈述都是一样的。我认为您实际上的意思是
,这将使它更加合乎逻辑。
这应该解决您的直接问题。但是我们可以进一步改善它。
通常最好先检查数据输入是有效的。因此,这应该首先完成。您甚至可以将其移至另一种方法。 (如果您有很多验证可以执行)。然后,您可以执行计算,
但是如果使用C#9或更高版本,则实际上可以将IF语句移至支持范围的Switch语句。在这种情况下,我们也可以将默认情况用于无效数据。
First, of we look at just one of your if statements we see the following
Well if
numberofchecks
equals 20, then it will always be <= 39, so the second part is redundant. And this is the same for all of your statments.I think what you actually mean is
Then, this would make it more logical.
This should solve your immediate problem. But we can improve it further.
It is generally better to first check the data input is valid. So this should be done first. You could even move this to another method. (Useful if you have lots of validation to perform). Then you can perform your calculation
But if you are using C#9 or above, you can actually move your if statements to a switch statement which supports ranges. And in this case, we can use the default case for the invalid data as well.
查看代码,似乎条件无法正确设置。
一个更好的有条件设置是始终首先检查不适当的案例。
因此,可以将以下检查范围降低到有效的检查范围。
Looking at the code, seems the conditions are not set up correctly.
A better conditional set up is to always check for inappropriate cases first.
So the following checks scope can be reduced to valid ones.