More concise case statements

Report a typo

Given the following statement:

switch (day) {
        case MONDAY:
        case FRIDAY:
        case SUNDAY:
            numLetters = 6;
            break;
        case TUESDAY:
            numLetters = 7;
            break;
        case THURSDAY:
        case SATURDAY:
            numLetters = 8;
            break;
        case WEDNESDAY:
            numLetters = 9;
            break;
        default:
            throw new IllegalStateException("Invalid day: " + day);
    }

The statement can be rewritten using switch expression. Complete the switch expression below. Write down only the case statement for MONDAY, FRIDAY, and SUNDAY. It should fit on one line. Use arrow notation.

int numLetters = switch (day) {
    // case for MONDAY, FRIDAY and SUNDAY
    // case for TUESDAY
    // case for THURSDAY and SATURDAY
    // case for WEDNESDAY
    default -> throw new IllegalStateException("Invalid day: " + day);
};
Enter a short text
___

Create a free account to access the full topic