我完全不知道如何用 C# 编写它。我知道如何在 Delphi 中完成。
这就是我在 Delphi 中的做法:
Case Total of
80..100 : ShowMessage ('You got an A!');
60..79 : ShowMessage ('You got a B!');
50..59 : ShowMessage ('You got a C!');
40..49 : ShowMessage ('You got a D!');
0..39 : ShowMessage ('You got an E...');
end;
我已经阅读了这方面的教程,但我不知道如何以我需要的方式使用/编写它。
这是我在 C# 中的版本:
switch (Total) //'Total' is a bunch of Integers divided together to get a Percentage
{
case 80..100: //Where 80% to 100% would achieve an A
{
MessageBox.Show("You got an A!");
}
case 60..79: //Where 60% to 79% would achieve a B
{
MessageBox.Show("You got a B!");
}
这可能吗?
谢谢。
最佳答案
要在 C# 中的 switch/case
语句中表达范围,您必须手动列出 case
switch (Total) {
case 80:
case 81:
...
case 100:
MessageBox.Show("you got an A");
break;
...
}
对于像这样的大范围,尽管使用一系列 if
语句可能会更好
if (Total >= 80 && Total <= 100) {
MessageBox.Show("you got an A");
} else if (Total >= 70) {
MessageBox.Show("you got a B");
}
关于c# - Case/switch 语句 c#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22426206/