C# 在两个数字之间切换?
C# Switch Between Two Numbers?
我正在尝试创建智能 switch 语句,而不是使用 20 多个 if 语句。我试过这个
private int num;
switch(num)
{
case 1-10:
Return "number is 1 through 10"
break;
default:
Return "number is not 1 through 10"
}
它说案件不能互相失败。
感谢您的帮助!
您尝试使用 switch/case 进行范围设置的语法是错误的。
case 1 - 10:
将被翻译成 case -9:
您可以通过两种方式尝试覆盖范围(多个值):
单独列出案例
case 1: case 2: case 3: case 4: case 5:
case 6: case 7: case 8: case 9: case 10:
return "Number is 1 through 10";
default:
return "Number is not 1 though 10";
计算范围
int range = (number - 1) / 10;
switch (range)
{
case 0: // 1 - 10
return "Number is 1 through 10";
default:
return "Number is not 1 though 10";
}
然而
您确实应该考虑使用 if
语句
覆盖值范围
if (1 <= number && number <= 10)
return "Number is 1 through 10";
else
return "Number is not 1 through 10";
不,switch case 中没有 "range" 的语法。如果您不想列出个别案例,那么 if/else 会更简洁:
if(num >= 1 && num <= 10)
Return "number is 1 through 10";
else
Return "number is not 1 through 10";
也可以用条件运算符缩短:
return (num >= 1 && num <= 10)
? "number is 1 through 10"
: "number is not 1 through 10";
我会使用最容易被其他人阅读和理解的那个。
通过最近的更改 introduced in C# 7,现在可以 switch
一个范围。
示例:
int i = 63;
switch (i)
{
case int n when (n >= 10):
Console.WriteLine($"I am 10 or above: {n}");
break;
case int n when (n < 10 && n >= 5 ):
Console.WriteLine($"I am between 10 and 5: {n}");
break;
case int n when (n < 5):
Console.WriteLine($"I am less than 5: {n}");
break;
}
注意:这对 OP 确实没有太大帮助,但希望它能帮助将来寻找它的人。
我知道我来晚了,但万一有人想知道这是怎么做到的,请看这个例子:
public string IsBetween1And10(int num)
{
return num switch
{
>= 1 and <= 10 => "number is 1 through 10",
_ => "number is not 1 through 10"
};
}
我正在尝试创建智能 switch 语句,而不是使用 20 多个 if 语句。我试过这个
private int num;
switch(num)
{
case 1-10:
Return "number is 1 through 10"
break;
default:
Return "number is not 1 through 10"
}
它说案件不能互相失败。
感谢您的帮助!
您尝试使用 switch/case 进行范围设置的语法是错误的。
case 1 - 10:
将被翻译成 case -9:
您可以通过两种方式尝试覆盖范围(多个值):
单独列出案例
case 1: case 2: case 3: case 4: case 5:
case 6: case 7: case 8: case 9: case 10:
return "Number is 1 through 10";
default:
return "Number is not 1 though 10";
计算范围
int range = (number - 1) / 10;
switch (range)
{
case 0: // 1 - 10
return "Number is 1 through 10";
default:
return "Number is not 1 though 10";
}
然而
您确实应该考虑使用 if
语句
if (1 <= number && number <= 10)
return "Number is 1 through 10";
else
return "Number is not 1 through 10";
不,switch case 中没有 "range" 的语法。如果您不想列出个别案例,那么 if/else 会更简洁:
if(num >= 1 && num <= 10)
Return "number is 1 through 10";
else
Return "number is not 1 through 10";
也可以用条件运算符缩短:
return (num >= 1 && num <= 10)
? "number is 1 through 10"
: "number is not 1 through 10";
我会使用最容易被其他人阅读和理解的那个。
通过最近的更改 introduced in C# 7,现在可以 switch
一个范围。
示例:
int i = 63;
switch (i)
{
case int n when (n >= 10):
Console.WriteLine($"I am 10 or above: {n}");
break;
case int n when (n < 10 && n >= 5 ):
Console.WriteLine($"I am between 10 and 5: {n}");
break;
case int n when (n < 5):
Console.WriteLine($"I am less than 5: {n}");
break;
}
注意:这对 OP 确实没有太大帮助,但希望它能帮助将来寻找它的人。
我知道我来晚了,但万一有人想知道这是怎么做到的,请看这个例子:
public string IsBetween1And10(int num)
{
return num switch
{
>= 1 and <= 10 => "number is 1 through 10",
_ => "number is not 1 through 10"
};
}