Java bootcamp · Lab 30

Days in a month

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 month 1–12; print days (non-leap).

Examples

Example 1
Input
2
Output
28
Example 2
Input
4
Output
30
Hint
  1. int[] days = {0,31,28,...}
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 m = in.nextInt();
    int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    System.out.println(days[m]);
  }
}
Main.javaJava 21 · javac · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.