Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package md.WK11toWK13;
- import java.util.Scanner;
- public class number03 {
- public static void main(String[] args) {
- Scanner ralph = new Scanner(System.in);
- int NumberOfDaysInMonth = 0;
- String NameOfMonth = "Unknown";
- System.out.print("Input a month number: ");
- int month = ralph.nextInt();
- System.out.print("Input a year: ");
- int year = ralph.nextInt();
- /*
- switch statement guide
- Number of Days in Month:
- January, March, May, July, August, October, December = 31
- February = 28 or 29
- April, June, September, November = 30
- Leap Year:
- if (( year % 400 is zero ) and (( year % 4 is zero ) or ( year % 100 is not zero))), Number of Days in February is 29
- else Number of Days in February is 28
- */
- switch (month) {
- case 1 -> {
- NameOfMonth = "January";
- NumberOfDaysInMonth = 31;
- }
- case 2 -> {
- NameOfMonth = "February";
- // leap year
- if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
- NumberOfDaysInMonth = 29;
- } else {
- NumberOfDaysInMonth = 28;
- }
- }
- case 3 -> {
- NameOfMonth = "March";
- NumberOfDaysInMonth = 31;
- }
- case 4 -> {
- NameOfMonth = "April";
- NumberOfDaysInMonth = 30;
- }
- case 5 -> {
- NameOfMonth = "May";
- NumberOfDaysInMonth = 31;
- }
- case 6 -> {
- NameOfMonth = "June";
- NumberOfDaysInMonth = 30;
- }
- case 7 -> {
- NameOfMonth = "July";
- NumberOfDaysInMonth = 31;
- }
- case 8 -> {
- NameOfMonth = "August";
- NumberOfDaysInMonth = 31;
- }
- case 9 -> {
- NameOfMonth = "September";
- NumberOfDaysInMonth = 30;
- }
- case 10 -> {
- NameOfMonth = "October";
- NumberOfDaysInMonth = 31;
- }
- case 11 -> {
- NameOfMonth = "November";
- NumberOfDaysInMonth = 30;
- }
- case 12 -> {
- NameOfMonth = "December";
- NumberOfDaysInMonth = 31;
- }
- }
- System.out.print(NameOfMonth + " " + year + " has " + NumberOfDaysInMonth + " days! ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement