C# 开关盒:我可以用一个范围而不是一个数字吗
我想用开关,但我有很多箱子,有没有捷径?到目前为止,我知道并尝试过的唯一解决方案是:C# 开关盒:我可以用一个范围而不是一个数字吗,c#,switch-statement,C#,Switch Statement,我想用开关,但我有很多箱子,有没有捷径?到目前为止,我知道并尝试过的唯一解决方案是: switch (number) { case 1: something; break; case 2: other thing; break; ... case 9: .........; break; } 我希望我能做的是: switch (number) { case (1 to 4): do the same for all of them; break; case (5 to 9): again, s
switch (number)
{
case 1: something; break;
case 2: other thing; break;
...
case 9: .........; break;
}
我希望我能做的是:
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;
}
如果使用C/C++,则没有“范围”语法。只能在每个“案例”段后列出所有值。
语言Ada或Pascal支持范围语法。首先,您应该指定所指的编程语言。
其次,
switch
语句正确地用于有关切换变量的闭合选项集,例如枚举或预定义字符串。对于这种情况,我建议使用良好的旧if-else
结构。您可以使用带有| |运算符(或运算符)的if-else语句,如:
通过
switch
case这是不可能的。您可以使用嵌套的if语句
if(number>=1 && number<=4){
//Do something
}else if(number>=5 && number<=9){
//Do something
}
如果(number>=1&&number=5&&number如果问题是关于C的(你没有说),那么答案是否定的,但是:GCC和Clang(可能其他)支持a,但它不是有效的ISO C:
switch (number) {
case 1 ... 4:
// Do something.
break;
case 5 ... 9:
// Do something else.
break;
}
请确保在…
前后都有一个空格,否则会出现语法错误。如前所述如果在这种情况下,您将处理一个范围:
if(number >= 1 && number <= 4)
{
//do something;
}
else if(number >= 5 && number <= 9)
{
//do something else;
}
if(number>=1&&number=5&&numberif else应该在这种情况下使用,但是如果出于任何原因仍然需要切换,您可以按以下操作,不中断的第一个案例将传播到遇到第一个中断。正如前面的回答所建议的,我建议使用if else切换
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!... }
};
在.Net中,只有Visual Basic允许在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;
}
然而,由于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;
}
if(百分比>=0&&percentage=40)
{
如果(百分比>=80)
{
//案例(80%至100%)
//中断;
}
其他的
{
如果(百分比>=70)
{
//个案(70%至79%)
//中断;
}
其他的
{
//个案(40%至69%)
//中断;
}
}
}
其他的
{
如果(百分比>=20)
{
//个案(20%至39%)
//中断;
}
其他的
{
//个案(0%至19%)
//中断;
}
}
}
其他的
{
//默认代码在这里
//中断;
}
习惯可能需要一点时间,但一旦习惯了就好了
就我个人而言,我欢迎switch语句允许范围
C#switch语句的未来
以下是我对如何改进switch语句的一些想法:
版本A
switch(value)
{
case (x => x >= 1 && x <= 4):
break;
case (x => x >= 5 && x <= 9):
break;
default:
break;
}
开关(值)
{
case(x=>x>=1&&x>=5&&x=1&¶m1=5&¶m1您可以通过将开关与边界的列表一起使用来构建“句柄”范围
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;
}
List bounds=newlist(){int.MinValue,0,4,9,17,20,int.MaxValue};
开关(bounds.IndexOf(bounds.Last(x=>x
使用这种方法,范围可以有不同的跨度。对于这个问题,游戏进行得有点晚,但在最近的更改中(默认情况下在Visual Studio 2017/.NET Framework 4.6.2中提供),现在可以使用switch
语句进行基于范围的切换
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!... }
};
示例:
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;
}
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
{
>= 1000000 => 7f,
>= 900000 => 7.1f,
>= 800000 => 7.2f,
_ => 0f // default value
};
inti=63;
开关(一)
{
当(n>=100)时的情况int n:
Console.WriteLine($“我100岁或以上:{n}”);
打破
当(n<100&&n>=50)时的情况:
WriteLine($“我在99和50之间:{n}”);
打破
案例int n(n<50):
WriteLine($“我小于50:{n}”);
打破
}
注意事项:
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;
}
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
{
>= 1000000 => 7f,
>= 900000 => 7.1f,
>= 800000 => 7.2f,
_ => 0f // default value
};
- 括号
(
和)
在when
条件中不是必需的,但在本例中用于突出显示比较
var
也可以用来代替int
。例如:当n>=100时,case var n:
我会使用三元运算符对切换条件进行分类
所以
在C#switch中,case基本上是关于下一步要做什么的字典。因为你无法在字典中查找范围,所以最好是case…当Steve Gomez提到语句时。要完成该线程,下面是C#8和C#9的语法:
C#8语法:
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;
}
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
{
>= 1000000 => 7f,
>= 900000 => 7.1f,
>= 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
};
var percent = price switch
{
>= 1000000 => 7f,
< 1000000 and >= 900000 => 7.1f,
< 900000 and >= 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
};
var percent = price switch
{
>= 1000000 => 7f,
< 1000000 and >= 900000 => 7.1f,
< 900000 and >= 800000 => 7.2f,
_ => 0f // default value
};
var%=价格转换
{
>=1000000=>7f,
<1000000和>=900000=>7.1f,
<900000和>=800000=>7.2f,
_=>0f//默认值
};
如果范围很小,您可以通过案例到达同一点。例如:
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;
}
int number=3;
开关(编号)
{
案例1:
案例2:
案例3:
案例4:
WriteLine(“数字是你可以在这样的场景中使用if-else。”)哇,这对于像if这样的东西来说有点复杂(number>=1&&number=5&&number哦,好的,是的……嗯,但是如果应该做相同事情的案例没有排序,那么您需要使用“| |运算符”……但他的目的是检查一个数字是否在某个范围内,因此顺序无关紧要(检查的范围除外)。这不是错误,您的解决方案没有错,只是比较复杂;-)==true:face\u palm:这是默认行为,for==false您可以使用not运算符。好的旧(数字>=1&&number哦,注意=
与=
的比较。Pascal/delphi也可以这样做。案例编号