所以我遇到了一个有趣的问题,我想用更有趣的方法来解决。所以我想做一个像这样工作的切换语句插入奇怪的假代码:
int i = 5;
switch (i)
case 0:
this line works
break;
case 1:
this line works
break;
case 2:
this line works
break;
case 3:
this line works
break;
case 4:
this line works
break;
case 5:
this line does not work
break;
case 6:
this line works
break;
忽略那些明显缺失的代码(我们都知道什么是switch语句(如果你没有那么我猜,欢迎来到这个奇怪的编程场所)),这里的主要问题是实现这样的case语句的问题,如图所示以上。编辑:如果你仍然不理解我的问题,我希望case语句输出所有其他情况但我选择的情况。
只需使用if
s:
int i = 5;
if (i != 0)
// this line works
if (i != 1)
// this line works
if (i != 2)
// this line works
if (i != 3)
// this line works
if (i != 4)
// this line works
if (i != 5)
// this line does not work
if (i != 6)
// this line works
不可能,开关只能做一个案例。你可以做的是添加for循环,如果你真的想要切换
int i = 5;
for(n=0 ; n <= 6 ; n++){
if(n == i){
n++;
}
switch (n)
case 0:
this line works
break;
case 1:
this line works
break;
case 2:
this line works
break;
case 3:
this line works
break;
case 4:
this line works
break;
case 5:
this line does not work
break;
case 6:
this line works
break;
}
这将跳过5