[英]Possible to simplify this conditional statement?
Is it possible to simplify the following statement using && or ||, or even & or | 是否可以使用&&或||,甚至&或|简化以下语句 or ^? 还是^?
int result = A ? (B ? 1 : -1) : (B ? -1 : 1);
I believe I could assign a temporary variable like so: 我相信我可以像这样分配一个临时变量:
boolean C = B ? A : !A; // Also: is it correct that this can be simplified to !(A ^ B)?
And then do: 然后执行:
int result = C ? 1 : -1;
But I'm curious if it's possible without a temporary variable. 但是我很好奇,如果没有临时变量,是否有可能。
您似乎已经有了答案。
int result = !(A ^ B) ? 1:-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.