Is there a simpler way to check multiple values against one value in an if-statement?

前端 未结 12 874
名媛妹妹
名媛妹妹 2020-11-27 07:55

Basically, what I want to do is check two integers against a given value, therefore, classically what you would do is something like this:

//just to get some         


        
12条回答
  •  失恋的感觉
    2020-11-27 08:04

    There is no special syntax for that. You could make a function for that. Assuming at least Java 1.5:

    public  boolean eitherOneEquals(T o1, T o2, T expectedValue) {
      return o1.equals(expectedValue) || o2.equals(expectedValue);
    }
    
    if(eitherOneEquals(o1, o2, expectedValue)) {
       // do something...
    }
    

提交回复
热议问题