Search code examples
javaeclipseintoperand

Or operand with int in if statement


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", "Right", "Left"};
} else {
    move = new Object[] {"Up", "Down", "Right", "Left"};
}

When hero position is 6, the program still goes to else.

Why is that? Is it because of operands? If yes, how should i change it?


Solution

  • Use:

    if (hero.getPos() == 6 || hero.getPos() == 11 || hero.getPos() == 16)) {
    

    This will do what you want.

    What you did is comparing hero.getPos() with the result of (6|11|16) which will do bitwise or between those numbers.