-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram53.java
More file actions
52 lines (44 loc) · 1.44 KB
/
Program53.java
File metadata and controls
52 lines (44 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
class DaysInMonth {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a month (1-12): ");
int month = input.nextInt();
System.out.print("Enter a year: ");
int year = input.nextInt();
int daysInMonth = getDaysInMonth(month, year);
if (daysInMonth == -1) {
System.out.println("Invalid month input. Please enter a number between 1 and 12.");
} else if (daysInMonth == -2) {
System.out.println("Invalid year input. Please enter a positive year.");
} else {
System.out.println("Number of days in the selected month: " + daysInMonth);
}
}
public static int getDaysInMonth(int month, int year) {
if (month < 1 || month > 12) {
return -1; // Invalid month input
}
if (year < 1) {
return -2; // Invalid year input
}
switch (month) {
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
default:
return 31;
}
}
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}