将你的评论转到@LeoCorrea的答案,你的条件似乎是布尔值.
也许尝试类似的东西:
final int PLAYER_BUST = 1 << 0;
final int DEALER_BUST = 1 << 1;
// ...
int total = 0;
total |= player.bust ? PLAYER_BUST : 0;
total |= dealer.bust ? DEALER_BUST : 0;
switch(total) {
case 0:
// neither player nor dealer has bust
break;
case PLAYER_BUST:
// just player bust, so dealer wins
break;
case DEALER_BUST:
// just dealer bust, so player wins
break;
case PLAYER_BUST | DEALER_BUST:
// both have bust
break;
}
1<<< x是2 ^ x,|是按位OR.我的例子是n = 2,但n可以是任何合理的数字. 这与how PHP encodes its error levels类似.请注意大多数级别如何用2的幂表示(二进制中只有一个’1’数字).
我们用|在这些水平上获得n个布尔结合的结果. int total中的每个位代表一个条件.
由于n个布尔值有2 ^ n个可能的组合结果,我们可以将每个结果与n位的整数相匹配.我们不能打开n布尔值,但幸运的是,我们可以使用这个代表n个布尔值的整数总和的开关.