开关:一种情况下有多个值?

时间:2012-10-16 09:38:45

标签: c# switch-statement case

我有以下代码,但是当我进入" 12"我仍然得到了#34;你是一个老人"。不是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;
  }

10 个答案:

答案 0 :(得分:36)

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 {
     ...
}

答案 1 :(得分:31)

您必须执行以下操作:

case 1:
case 2:
case 3:
//do stuff
break;

答案 2 :(得分:6)

您无法在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;

...........等

答案 3 :(得分:4)

你可以改用ifelse。但如果你想知道如何在这种情况下使用开关。这是一个例子。

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;
}
希望有所帮助! :)

答案 4 :(得分:3)

在C#7中,可以使用when clause in a case statement

ng build --prod --extract-css=false

答案 5 :(得分:0)

你可以试试这个。

switch (Valor)
            {
                case (Valor1 & Valor2):

                    break;               
            }

答案 6 :(得分:0)

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

答案 7 :(得分: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");
}   

`

答案 8 :(得分: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;
}

欢呼

答案 9 :(得分:0)

C# 8.0 中,您可以使用适合您情况的 switch expression 语法。

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