使用以下代码时,我遇到一些转换问题
int mode = 4;
if (mode & 1) { // Getting conversion issue from this line
x = 0;
if (mode & 4 ) {y = -ry ;} else {y = ry;};
}
如何解决这个问题?有什么建议吗?
最佳答案
mode & 1
被评估为int
,并且不能转换为boolean
(这是if
语句的表达式的预期类型)。
假设您想测试是否mode & 1 > 0
,则应输入:
if ((mode & 1) > 0) // tests if the lowest bit of mode is 1
然后
if ((mode & 4) > 0) // tests if the 3rd lowest bit of mode is 1
关于java - 无法在Java中从int转换为boolean,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32473918/