【发布时间】:2016-01-21 00:04:20
【问题描述】:
所以基本上,我试图让 switch 语句只执行一次。并且不执行所有这些。 假设玩家在他们的库存中拥有所有的钥匙。 我只想执行一个案例,而不是遍历所有案例。 (我只想看到一个执行)
public int[] allKeys = {1543, 1544, 1545, 1546, 1547, 1548};
if (player.getInventory().containsAny(allKeys)) {
for (int id : allKeys) {
switch(id) {
case 1543:
System.out.println("executed");
break;
case 1544:
System.out.println("executed");
break;
case 1545:
System.out.println("executed");
break;
case 1546:
System.out.println("executed");
break;
case 1547:
System.out.println("executed");
break;
case 1548:
System.out.println("executed");
break;
default:
System.out.println("error!");
}
}
}
【问题讨论】:
-
去掉for循环就行了
标签: switch-statement break execution