If you really want to use switch statements, here is a way to create pseudo ranges with enum s, so you can switch enumerations.
First we need to create 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 getFrom(int score) { return Arrays.asList(Range.values()).stream() .filter(t -> (score >= t.minValue && score <= t.maxValue)) .findAny() .orElse(OTHER); } }
And then your switch:
int num = 1630; switch (Range.getFrom(num)) { case TWO_HUNDRED: // Do something break; case SIXTEEN_HUNDRED: // Do another thing break; case OTHER: default: // Do a whole different thing break; }
source share