Skip to content

Instantly share code, notes, and snippets.

@arindamroynitw
Created September 1, 2019 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arindamroynitw/b7c80d0d873e2e6899d6068013f8ec70 to your computer and use it in GitHub Desktop.
Save arindamroynitw/b7c80d0d873e2e6899d6068013f8ec70 to your computer and use it in GitHub Desktop.
import static java.util.Calendar.*;
public class NewSwitch {
public static void main(String[] args) {
int numberOfDays = getNumberOfDays(MARCH);
System.out.println("The number of days in this month are " + numberOfDays);
}
public static int getNumberOfDays(int monthNum) {
return switch (monthNum) {
case JANUARY, MARCH, MAY, JULY, AUGUST, OCTOBER, DECEMBER -> 31;
case APRIL, JUNE, SEPTEMBER, NOVEMBER -> 30;
case FEBRUARY -> 28; //assuming nin leap year for simplicity
default -> throw new IllegalArgumentException("Not a month");
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment