Bet*_*Bet 10 java boolean-logic xor
例如,
if (bool1 ^ bool2 ^ bool3 ^ bool4)
{
// Do whatever
}
Run Code Online (Sandbox Code Playgroud)
只有在满足其中一个条件时才应执行.
将bool作为整数添加在一起并检查它们是否等于1.
在从布尔到整数的转换不起作用的语言中,例如Java,更长的winded选项是:
if ((bool1 ? 1 : 0) + (bool2 ? 1 : 0) + (bool3 ? 1 : 0) + (bool4 ? 1 : 0) == 1) {
// only runs when one of bool 1-4 is true
}
Run Code Online (Sandbox Code Playgroud)
但是,在将布尔值转换为整数的其他语言中,您可以执行以下操作:
if ((int)(bool1) + (int)(bool2) + (int)(bool3) + (int)(bool4) == 1) {
// only runs when one of bool 1-4 is true
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
230 次 |
最近记录: |