Can I have a switch statement like this:
我可以使用这样的switch语句:
...
switch (temp)
{
case "NW" or "New":
temp = "new stuff"
break;
}
...
6 个解决方案
#1
No, but you can use (at least in Java)
不,但你可以使用(至少在Java中)
switch (temp) {
case "NW":
case "New":
temp="new stuff";
break;
}
#2
Yes. This is how it is done.
是。这就是它的完成方式。
switch (temp)
{
case "NW":
case "New":
temp = "new stuff"
break;
}
Actually, I answered this very same question before.
实际上,我之前回答了同样的问题。
#3
Try
switch (temp)
{
case "NW":
case "New":
temp = "new stuff"
break;
}
#4
Assuming C#, you want:
假设C#,你想要:
switch(temp)
{
case "NW":
case "New":
temp = "new stuff";
break;
}
#5
switch (temp) {
case "NW":
case "New":
temp = "new stuff"
break;
default:
Console.WriteLine("Hello, World!");
break;
}
#6
I know you asked about C#, and have good answers there, but just for perspective (and for anyone else reading that might find it useful), here's the VB answer:
我知道你问过C#,并且在那里有很好的答案,但只是为了透视(对于其他任何阅读可能会觉得有用的人),这是VB的答案:
Select Case temp
Case "NW", "New"
temp = "new stuff"
Case Else
'something else...
End Select
Notice that there's no "break"--VB does not drop through cases. On the other hand, you can have multiple match conditions on a single case.
请注意,没有“中断” - VB不会丢失案例。另一方面,您可以在一个案例中拥有多个匹配条件。
Be care you DON'T do this
小心你不要这样做
...
Case "NW" Or "New"
...
What you have there is a single condition with a bitwise Or between the two terms....
你在那里有一个单一的条件,在两个术语之间有一个按位或......
#1
No, but you can use (at least in Java)
不,但你可以使用(至少在Java中)
switch (temp) {
case "NW":
case "New":
temp="new stuff";
break;
}
#2
Yes. This is how it is done.
是。这就是它的完成方式。
switch (temp)
{
case "NW":
case "New":
temp = "new stuff"
break;
}
Actually, I answered this very same question before.
实际上,我之前回答了同样的问题。
#3
Try
switch (temp)
{
case "NW":
case "New":
temp = "new stuff"
break;
}
#4
Assuming C#, you want:
假设C#,你想要:
switch(temp)
{
case "NW":
case "New":
temp = "new stuff";
break;
}
#5
switch (temp) {
case "NW":
case "New":
temp = "new stuff"
break;
default:
Console.WriteLine("Hello, World!");
break;
}
#6
I know you asked about C#, and have good answers there, but just for perspective (and for anyone else reading that might find it useful), here's the VB answer:
我知道你问过C#,并且在那里有很好的答案,但只是为了透视(对于其他任何阅读可能会觉得有用的人),这是VB的答案:
Select Case temp
Case "NW", "New"
temp = "new stuff"
Case Else
'something else...
End Select
Notice that there's no "break"--VB does not drop through cases. On the other hand, you can have multiple match conditions on a single case.
请注意,没有“中断” - VB不会丢失案例。另一方面,您可以在一个案例中拥有多个匹配条件。
Be care you DON'T do this
小心你不要这样做
...
Case "NW" Or "New"
...
What you have there is a single condition with a bitwise Or between the two terms....
你在那里有一个单一的条件,在两个术语之间有一个按位或......