If statement, compare one variable to multiple

后端 未结 6 1317
挽巷
挽巷 2021-01-22 10:53

Example:

int value = someValue;
if (value == (valueOne OR valueTwo OR valueThree)){
//do code
}

I would like to avoid re-typing value

6条回答
  •  温柔的废话
    2021-01-22 11:13

    You can't do this with if statement in Java, but with the switch statement.

    switch (value) {
       case 1:
       case 2:
       case 3: {
          //do something
       }
    }
    

    This will check if the value is equal to 1, 2 or 3.

提交回复
热议问题