You could...
Use a switch statement...
switch (name) {
case "fred":
case "bob":
case "jack":
break;
}
And
switch (numb) {
case 4:
case 45:
case 91:
break;
}
You could...
Use a Collection
of some type...
if (Arrays.asList("fred", "bob", "jack").contains("fred")) {
// Have match
}
Which is useful if you have dynamic values...
You could also use something like...
if (Arrays.asList("fred", "bob", "jack").containsAll(Arrays.asList("fred", "bob", "jack"))) {
// Have match
}
If you need to to use &&
instead of ||
...
You could also substitute the long hand String
list with a predefined array or List
, which could allow you to write a single method...
public <T> boolean matchOR(List<T> want, T have) {
return want.contains(have);
}
public <T> boolean matchAnd(List<T> want, List<T> have) {
return want.containsAll(have);
}
Which would allow you to write something like...
if (matchOr(Arrays.asList("fred", "bob", "jack"), name)) {
//...
}
As an idea...
At some point, context will need to be considered, for example, the above examples allow you to provide a flexible series of conditions, but you might not want to do this and will have to write your if
statements as you have been