Java bootcamp · Lab 33

Day name

easySwitch10 minLesson: Switch

Read the question, write Java on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read 1–7; print day name or Invalid.

Examples

Example 1
Input
1
Output
Monday
Example 2
Input
0
Output
Invalid
Hint
  1. switch (d) { case 1: ... }
Show correct code

Peek only after you have tried. You can still Check your own version.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int d = in.nextInt();
    String[] days = {"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    System.out.println(d >= 1 && d <= 7 ? days[d] : "Invalid");
  }
}
Main.javaJava 21 · javac · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.