如何在相同的情况下添加多个值

时间:2014-01-24 07:51:53

标签: c# switch-statement case multivalue

在将多个值添加到同一个案例中时遇到问题:

这是我的c#代码

string input = combobox1.selectedvalue.ToString(); 
switch(input)
{
case "one";
     return 1;
     break;
case "two";
     return 2;
     break;
case "three" , "four":   // error here
     return 34;
     break;
default:
     return 0;
}

需要你的帮助

3 个答案:

答案 0 :(得分:5)

只需使用单独的标签:

string input = combobox1.selectedvalue.ToString(); 
switch(input)
{
case "one":
     return 1;
     break;
case "two":
     return 2;
     break;
case "three": 
case "four":
     return 34;
     break;
default:
     return 0;
}

请参阅switch

  

每个开关部分包含一个或多个案例标签,后跟一个或多个语句

答案 1 :(得分:1)

你可以摔倒,请阅读this了解更多信息 所以它看起来像这样

switch(input)
{
case "one":
     return 1;
     break;
case "two":
     return 2;
     break;
case "three":
case "four": 
     return 34;
     break;
default:
     return 0;
}

答案 2 :(得分:0)

正确的语法是

case "three": 
case "four":
     return 34;
     break;

代替

case "three" , "four": 
     return 34;
     break;

来自switch (C# Reference)

  

switch语句包括一个或多个开关部分。每个开关   section包含一个或多个案例标签,后跟一个或多个案例标签   语句。