Answer:
Explanation:
The following code is written in Java and uses Switch statements to connect the month to the correct number of days. It uses the year value to calculate the number of days only for February since it is the only month that actually changes. Finally, the number of days is printed to the screen.
import java.util.Scanner;
public class NumDaysPerezGabriel {
public static void main(String args[]) {
int totalDays = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter number for month (Ex: 01 for January or 02 for February): ");
int month = in.nextInt();
System.out.println("Enter 4 digit number for year: ");
int year = in.nextInt();
switch (month) {
case 1:
totalDays = 31;
break;
case 2:
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
totalDays = 29;
} else {
totalDays = 28;
}
break;
case 3:
totalDays = 31;
break;
case 4:
totalDays = 30;
break;
case 5:
totalDays = 31;
break;
case 6:
totalDays = 30;
break;
case 7:
totalDays = 31;
break;
case 8:
totalDays = 31;
break;
case 9:
totalDays = 30;
break;
case 10:
totalDays = 31;
break;
case 11:
totalDays = 30;
break;
case 12:
totalDays = 31;
}
System.out.println("There are a total of " + totalDays + " in that month for the year " + year);
}
}