Or operand with int in if statement

前端 未结 12 1942
傲寒
傲寒 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:08

    You could put all the numbers in a collection, and then use the contains() method. Other than that, I don't believe there is any special syntax for comparing like you want to do.

    0 讨论(0)
  • 2021-01-25 08:10

    There is no such operator. But if you are comparing number, you can use switch do simulate that. Here is how:

    int aNumber = ci.getNumber();
    swithc(aNumber) {
        case 6252001:
        case 5855797:
        case 6251999: {
            ...
            break;
        }
        default: {
            ... // Do else.
        }
    }
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-25 08:13
    boolean theyAretheSame = num1 == num2 ? (num1 == num3 ? true:false):false;
    

    I must admit I haven't checked this but the logic looks correct.

    0 讨论(0)
  • 2021-01-25 08:16

    using the answer from:

    How can I test if an array contains a certain value?

    you could create an array of numbers and check if your ci.getNumber() is in it.

    0 讨论(0)
  • 2021-01-25 08:18

    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.

    0 讨论(0)
  • 2021-01-25 08:20

    The other answers are correct, just thinking differently you may use Sets.

    static final Set<Integer> positions = new HashSet<Integer>();
    static{
        positions.add(6);
        positions.add(11);
        positions.add(16);
    }
    
    if (positions.contains(hero.getPos())){
        move = new Object[] {"Up", "Right", "Left"};
    } else {
        move = new Object[] {"Up", "Down", "Right", "Left"};
    }
    
    0 讨论(0)
提交回复
热议问题