Or operand with int in if statement

前端 未结 12 2065
傲寒
傲寒 2021-01-25 07:45

My problem is that program is not reading codes as i intended \"he\" would.

I have

if (hero.getPos() == (6 | 11 | 16)) {
    move = new Object[] {\"Up\",         


        
12条回答
  •  轮回少年
    2021-01-25 08:02

    (6 | 11 | 16) would be evaluated first to 31 (binary operation), which is 6 != 31. Not what you want.

    Better is to check every single position (you have only 3, so inline is good, for more consider using a loop):

    if (hero.getPos() == 6 || hero.getPos() == 11 | hero.getPos() == 16)) {
        move = new Object[] {"Up", "Right", "Left"};
    } else {
        move = new Object[] {"Up", "Down", "Right", "Left"};
    }
    

提交回复
热议问题