开关情况: 我可以使用一个范围,而不是一个数字

我想用开关,但我有很多情况下,有什么捷径吗?到目前为止,我知道并尝试过的唯一解决办法是:

switch (number)
{
case 1: something; break;
case 2: other thing; break;
...
case 9: .........; break;
}

What I hope I'm able to do is something like:

switch (number)
{
case (1 to 4): do the same for all of them; break;
case (5 to 9): again, same thing for these numbers; break;
}
162536 次浏览

如果使用 C/C + + ,没有“ range”语法,只能在每个“ case”段后列出所有值。 语言 Ada 或 Pascal 支持范围语法。

首先,您应该指定所引用的编程语言。 其次,switch语句适用于关于切换变量的闭合选项集,例如枚举或预定义字符串。对于这种情况,我建议使用旧的 if-else结构。

可以对 | | 运算符(或-运算符)使用 if-else 语句,如:

if(case1 == true || case2 == true || case3 == true)
{
Do this!...
}
else if(case4 == true || case5 == true || case6 == true)
{
Do this!...
}
else if(case7 == true || case8 == true || case9 == true)
{
Do this!...
}

通过 switch的情况是不可能的。您可以使用嵌套的 if 语句。

if(number>=1 && number<=4){
//Do something
}else if(number>=5 && number<=9){
//Do something
}

如果问题是关于 C 的(你没有说) ,那么答案是 没有,但是: GCC 和 Clang (也许是其他的)支持 范围语法,但它是 没有有效的 ISO C:

switch (number) {
case 1 ... 4:
// Do something.
break;


case 5 ... 9:
// Do something else.
break;
}

请确保在 ...之前和之后都有一个空格,否则您将得到一个语法错误。

正如前面提到的,在这种情况下,if-else会更好,您将处理一个范围:

if(number >= 1 && number <= 4)
{
//do something;
}
else if(number >= 5 && number <= 9)
{
//do something else;
}

在这种情况下应该使用 If-else,但是如果由于任何原因仍然需要开关,可以按照下面的方法,没有中断的第一个情况将会传播,直到遇到第一个中断。正如前面的答案所建议的,我推荐使用 if-else 而不是 switch。

switch (number){
case 1:
case 2:
case 3:
case 4: //do something;
break;
case 5:
case 6:
case 7:
case 8:
case 9: //Do some other-thing;
break;
}

间隔是恒定的:

 int range = 5
int newNumber = number / range;
switch (newNumber)
{
case (0): //number 0 to 4
break;
case (1): //number 5 to 9
break;
case (2): //number 10 to 14
break;
default:  break;
}

否则:

  if else

这里有一个更好、更优雅的问题陈述解决方案。

int mynumbercheck = 1000;
// Your number to be checked
var myswitch = new Dictionary <Func<int,bool>, Action>
{
{ x => x < 10 ,    () => //Do this!...  },
{ x => x < 100 ,    () => //Do this!...  },
{ x => x < 1000 ,    () => //Do this!...  },
{ x => x < 10000 ,   () => //Do this!... } ,
{ x => x < 100000 ,  () => //Do this!... },
{ x => x < 1000000 ,  () => //Do this!... }
};

现在调用我们的条件开关

   myswitch.First(sw => sw.Key(mynumbercheck)).Value();

用于 Switch/ifElse 的备用

在.Net 中,只有 VisualBasic 允许在 switch 语句中使用范围,但是在 C # 中没有这样的有效语法。

解决你在 C # 中遇到的具体问题,我会这样解决:

if(number >= 1 && number <= 9) // Guard statement
{
if(number < 5)
{
// Case (1 to 4):


//break;


}
else
{
// Case (5 to 9):


//break;


}


}
else
{
// Default code goes here


//break;


}

为了进一步说明这一点,假设您有一个百分比值。

将您的问题作为模板,您可能希望这样:

switch (percentage)
{
case (0 to 19):
break;


case (20 to 39):
break;


case (40 to 69):
break;


case (70 to 79):
break;


case (80 to 100):
break;


default:
break;


}

然而,由于 C # 不允许这种语法,这里有一个 C # 允许的解决方案:

if (percentage >= 0 && percentage <= 100) // Guard statement
{
if (percentage >= 40)
{
if (percentage >= 80)
{
// Case (80% to 100%)


//break;


}
else
{
if (percentage >= 70)
{
// Case (70% to 79%)


//break;


}
else
{
// Case (40% to 69%)


//break;


}


}


}
else
{
if (percentage >= 20)
{
// Case (20% to 39%)


//break;


}
else
{
// Case (0% to 19%)


//break;


}


}


}
else
{
// Default code goes here


//break;


}

可能需要一点时间来适应,但是一旦你习惯了就没事了。

就个人而言,我欢迎切换语句以允许范围。

C # switch 语句的未来

以下是我对如何改进 switch 语句的一些想法:

版本 A

switch(value)
{
case (x => x >= 1 && x <= 4):
break;


case (x => x >= 5 && x <= 9):
break;


default:
break;


}

版本 B

switch(param1, param2, ...)
{
case (param1 >= 1 && param1 <= 4):
break;


case (param1 >= 5 && param1 <= 9 || param2 != param1):
break;


default:
break;


}

您可以将 switch构造“句柄”范围与边界的 List结合使用。

List<int> bounds = new List<int>() {int.MinValue, 0, 4, 9, 17, 20, int.MaxValue };


switch (bounds.IndexOf(bounds.Last(x => x < j)))
{
case 0: // <=0
break;


case 1: // >= 1 and <=4
break;
case 2: // >= 5 and <=9
break;
case 3: // >= 10 and <=17
break;
case 4: // >= 18 and <=20
break;


case 5: // >20
break;
}

使用这种方法,范围可以有不同的跨度。

C # 7的原始答案

这个问题有点晚了,但是最近改动了 在 C # 7中引入(在 Visual Studio 2017/默认情况下可用)。NET Framework 4.6.2) ,现在可以使用 switch语句进行基于范围的切换。

例如:

int i = 63;


switch (i)
{
case int n when (n >= 100):
Console.WriteLine($"I am 100 or above: {n}");
break;


case int n when (n < 100 && n >= 50 ):
Console.WriteLine($"I am between 99 and 50: {n}");
break;


case int n when (n < 50):
Console.WriteLine($"I am less than 50: {n}");
break;
}

备注:

  • when条件中不需要括号 (),但是在本例中使用它们来突出显示比较。
  • var也可以用来代替 int。例如: case var n when n >= 100:

C # 9的更新示例

switch(myValue)
{
case <= 0:
Console.WriteLine("Less than or equal to 0");
break;
case > 0 and <= 10:
Console.WriteLine("More than 0 but less than or equal to 10");
break;
default:
Console.WriteLine("More than 10");
break;
}

或者

var message = myValue switch
{
<= 0 => "Less than or equal to 0",
> 0 and <= 10 => "More than 0 but less than or equal to 10",
_ => "More than 10"
};
Console.WriteLine(message);

我将使用三元运算符来分类您的开关条件。

那么..。

switch( number > 9 ? "High" :
number > 5 ? "Mid" :
number > 1 ? "Low" : "Floor")
{
case "High":
do the thing;
break;
case "Mid":
do the other thing;
break;
case "Low":
do something else;
break;
case "Floor":
do whatever;
break;
}

在 C # switch 案例中,基本上是关于下一步做什么的字典。因为你不能在字典中查找一个范围,所以你能做的最好的事情就是,当史蒂夫 · 戈麦斯提到。

为了完成这个线程,下面是 C # 8和 C # 9的语法:

C # 9语法:

var percent = price switch
{
>= 1000000 => 7f,
>= 900000 => 7.1f,
>= 800000 => 7.2f,
_ => 0f // default value
};

如果要指定范围:

var percent = price switch
{
>= 1000000 => 7f,
< 1000000 and >= 900000 => 7.1f,
< 900000 and >= 800000 => 7.2f,
_ => 0f // default value
};

C # 8语法:

var percent = price switch
{
var n when n >= 1000000 => 7f,
var n when n >= 900000 => 7.1f,
var n when n >= 800000 => 7.2f,
_ => 0f // default value
};

如果要指定范围:

var percent = price switch
{
var n when n >= 1000000 => 7f,
var n when n < 1000000 && n >= 900000 => 7.1f,
var n when n < 900000 && n >= 800000 => 7.2f,
_ => 0f // default value
};

如果范围很小,你可以通过案例来达到同样的效果。例如:

int number = 3;
switch (number)
{
case 1:
case 2:
case 3:
case 4:
Console.WriteLine("Number is <= 1 <= 4");
break;
case 5:
Console.WriteLine("Number is 5");
break;
default:
Console.WriteLine("Number is anything else");
break;
}