If you really want to use switch statements — here is a way to create pseudo-ranges with enum
s, so you can switch over the enums.
First, we'll need to create the ranges:
public enum Range {
TWO_HUNDRED(200, 299),
SIXTEEN_HUNDRED(1600, 1699),
OTHER(0, -1); // This range can never exist, but it is necessary
// in order to prevent a NullPointerException from
// being thrown while we switch
private final int minValue;
private final int maxValue;
private Range(int min, int max) {
this.minValue = min;
this.maxValue = max;
}
public static Range from(int score) {
return Arrays.stream(Range.values())
.filter(range -> score >= range.minValue && score <= range.maxValue)
.findAny()
.orElse(OTHER);
}
}
And then your switch:
int num = 1630;
switch (Range.from(num)) {
case TWO_HUNDRED:
// Do something
break;
case SIXTEEN_HUNDRED:
// Do another thing
break;
case OTHER:
default:
// Do a whole different thing
break;
}