C# 开关:一种情况下有多个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12911691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch: Multiple values in one case?
提问by vlovystack
I have the following piece of code, but yet when I enter "12" I still get "You an old person". Isn't 9 - 15 the numbers 9 UNTIL 15? How else do I handle multiple values with one case?
我有以下一段代码,但是当我输入“12”时,我仍然得到“你是一个老人”。9 - 15 不是 9 到 15 的数字吗?我如何处理一个案例的多个值?
int age = Convert.ToInt32(txtBoxAge.Text);
switch (age)
{
case 1 - 8:
MessageBox.Show("You are only " + age + " years old\n You must be kidding right.\nPlease fill in your *real* age.");
break;
case 9 - 15:
MessageBox.Show("You are only " + age + " years old\n That's too young!");
break;
case 16-100:
MessageBox.Show("You are " + age + " years old\n Perfect.");
break;
default:
MessageBox.Show("You an old person.");
break;
}
采纳答案by Marc Gravell
1 - 8 = -7
1 - 8 = -7
9 - 15 = -6
9 - 15 = -6
16 - 100 = -84
16 - 100 = -84
You have:
你有:
case -7:
...
break;
case -6:
...
break;
case -84:
...
break;
Eitheruse:
要么使用:
case 1:
case 2:
case 3:
etc, or(perhaps more readable) use:
等等,或者(也许更易读)使用:
if(age >= 1 && age <= 8) {
...
} else if (age >= 9 && age <= 15) {
...
} else if (age >= 16 && age <= 100) {
...
} else {
...
}
etc
等等
回答by Tomtom
You have to do something like:
您必须执行以下操作:
case 1:
case 2:
case 3:
//do stuff
break;
回答by Basil Farook
You can't specify a range in the case statement, can do as follows.
你不能在case语句中指定一个范围,可以做如下。
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
MessageBox.Show("You are only " + age + " years old\n You must be kidding right.\nPlease fill in your *real* age.");
break;
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
MessageBox.Show("You are only " + age + " years old\n That's too young!");
break;
...........etc.
...........等等。
回答by Sahi Sahith
You can use ifelse instead.but if you want to know how to use switch in this case.here is an example.
您可以改用 ifelse。但是如果您想知道在这种情况下如何使用 switch。这里是一个示例。
int age = Convert.ToInt32(txtBoxAge.Text);`
int flag;
if(age >= 1 && age <= 8) {
flag = 1;
} else if (age >= 9 && age <= 15) {
flag = 2;
} else if (age >= 16 && age <= 100) {
flag = 3;
} else {
flag = 4;
}
switch (flag)
{
case 1:
MessageBox.Show("You are only " + age + " years old\n You must be kidding right.\nPlease fill in your *real* age.");
break;
case 2:
MessageBox.Show("You are only " + age + " years old\n That's too young!");
break;
case 3:
MessageBox.Show("You are " + age + " years old\n Perfect.");
break;
default:
MessageBox.Show("You an old person.");
break;
}
hope that helps ! :)
希望有帮助!:)
回答by Francisco
you can try this.
你可以试试这个。
switch (Valor)
{
case (Valor1 & Valor2):
break;
}
回答by Carl Fredrickson
There's no way to evaluate multiple values in one 'case'. You could either use an if statement (as others have suggested) or call a method which evaluates the range that the integer belongs to and returns a value which represents that range (such as "minor", "adult", etc.), then evaluate this returned value in the switch statement. Of course, you'd probably still be using an if statement in the custom method.
无法在一个“案例”中评估多个值。您可以使用 if 语句(正如其他人所建议的那样)或调用一个方法来评估整数所属的范围并返回一个代表该范围的值(例如“未成年人”、“成人”等),然后在 switch 语句中评估这个返回值。当然,您可能仍会在自定义方法中使用 if 语句。
回答by bkstill
In C# 7 it's possible to use a when clause in a case statement.
在 C# 7 中,可以在 case 语句中使用when 子句。
int age = 12;
switch (age)
{
case int i when i >=1 && i <= 8:
System.Console.WriteLine("You are only " + age + " years old. You must be kidding right. Please fill in your *real* age.");
break;
case int i when i >=9 && i <= 15:
System.Console.WriteLine("You are only " + age + " years old. That's too young!");
break;
case int i when i >=16 && i <= 100:
System.Console.WriteLine("You are " + age + " years old. Perfect.");
break;
default:
System.Console.WriteLine("You an old person.");
break;
}
回答by brewmanz
Separate the business rules for age from the actions e.g. (NB just typed, not checked)
将年龄的业务规则与操作分开,例如(注意只是输入,未检查)
enum eAgerange { eChild, eYouth, eAdult, eAncient};
eAgeRange ar;
if(age <= 8) ar = eChild;
else if(age <= 15) ar = eYouth;
else if(age <= 100) ar = eAdult;
else ar = eAncient;
switch(ar)
{
case eChild:
// action
case eYouth:
// action
case eAdult:
// action
case eAncient:
// action
default: throw new NotImplementedException($"Oops {ar.ToString()} not handled");
}
`
`
回答by Lee Traynor
What about this?
那这个呢?
switch (true)
{
case (age >= 1 && age <= 8):
MessageBox.Show("You are only " + age + " years old\n You must be kidding right.\nPlease fill in your *real* age.");
break;
case (age >= 9 && age <= 15):
MessageBox.Show("You are only " + age + " years old\n That's too young!");
break;
case (age >= 16 && age <= 100):
MessageBox.Show("You are " + age + " years old\n Perfect.");
break;
default:
MessageBox.Show("You an old person.");
break;
}
Cheers
干杯

