31

我有以下一段代码,但是当我输入“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;
  }
4

10 回答 10

41

1 - 8 = -7

9 - 15 = -6

16 - 100 = -84

你有:

case -7:
    ...
    break;
case -6:
    ...
    break;
case -84:
    ...
    break;

要么使用:

case 1:
case 2: 
case 3:

等,或者(也许更易读)使用:

if(age >= 1 && age <= 8) {
     ...
} else if (age >= 9 && age <= 15) {
     ...
} else if (age >= 16 && age <= 100) {
     ...
} else {
     ...
}

ETC

于 2012-10-16T09:40:52.517 回答
35

您必须执行以下操作:

case 1:
case 2:
case 3:
//do stuff
break;
于 2012-10-16T09:40:38.993 回答
8

您不能在 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。

于 2012-10-16T09:49:30.257 回答
8

在 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;
}
于 2018-04-10T15:36:36.787 回答
5

您可以使用 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;
}

希望有帮助!:)

于 2015-03-31T07:39:26.860 回答
2

C# 8.0您可以使用适合您情况的switch表达式语法。

int age = Convert.ToInt32(txtBoxAge.Text);

string message = age switch
{
    >= 1 and <= 8 => $"You are only {age} years old\n You must be kidding right.\nPlease fill in your *real* age.",
    >= 9 and <= 15 => $"You are only {age} years old\n That's too young!",
    >= 16 and <= 100 => $"You are {age} years old\n Perfect.",
    _ => "You an old person.",
};

MessageBox.Show(message);
于 2021-07-16T18:35:56.883 回答
0

你可以试试这个。

开关(勇气)
            {
                案例(Valor1 和 Valor2):

                    休息;               
            }
于 2016-08-03T16:41:23.453 回答
0

没有办法在一个“案例”中评估多个值。您可以使用 if 语句(如其他人所建议的那样)或调用一个方法来评估整数所属的范围并返回一个表示该范围的值(例如“minor”、“adult”等),然后在 switch 语句中评估这个返回值。当然,您可能仍在自定义方法中使用 if 语句。

于 2016-12-16T21:27:40.233 回答
0

将年龄的业务规则与操作分开,例如(NB 只是输入,未检查)

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");
}   

`

于 2019-06-05T00:57:13.573 回答
0

那这个呢?

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;
}

干杯

于 2019-12-13T12:29:51.397 回答