If statement, compare one variable to multiple

后端 未结 6 1378
挽巷
挽巷 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:11

    You might be better off using a HashSet

     Set myset = new HashSet();
    //Add values to your set
    if(myset.contains(myvalue));
    {
        //... Do what you want.
    

    This allows your algorithm to be flexible and not have to manually code in new checks when you want to check against a new value.

提交回复
热议问题