Java

[Java] 윤년 여부 판단, 유효 날짜 판단 코드

15호의 개발자 2021. 8. 5. 14:40
반응형

[Java] 윤년 여부 판단, 유효 날짜 판단 코드

 

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.Scanner;
 
public class MyDate {
 
    public static int day;
    public static int month;
    public static int year;
    public static boolean isValid = false;
    public static boolean isComYear = false;
    
    public static void main(String[] args) {
 
        // 날짜 입력 받기
        System.out.println("'일 월 년' 순으로 입력해주세요.");
        
        Scanner scanner = new Scanner(System.in);
        int day, month, year;
        day = scanner.nextInt();
        month = scanner.nextInt();
        year = scanner.nextInt();
        
        System.out.println(day + " " + month + " " + year);
        
        // 윤년 여부 판단, 유효 날짜 판단
        if ( year % 4 == 0 && year % 100 == 0 ) {
            if ( year % 400 == 0) { // 윤년
                isComYear = false;
                isCommonYear();
            }
            else { // 평년
                isComYear = true;
                isLeapYear();
            }
        }
        else if ( year % 4 == 0) { // 윤년
            isComYear = false;
            isLeapYear();
        }
        else { // 평년
            isComYear = true;
            isCommonYear();
        }
        
        // 프린트문
        if (isComYear == true && isValid == true) {
            System.out.println( year + "년은 평년이며, "+ month + "월 "
                                + day + "일은 유효한 날짜입니다.");
        }
        else if (isComYear == true && isValid == false) {
            System.out.println( year + "년은 평년이며, "+ month + "월 "
                                + day + "일은 유효하지 않은 날짜입니다.");
        }
        else if (isComYear == false && isValid == true) {
            System.out.println( year + "년은 윤년이며, "+ month + "월 "
                                + day + "일은 유효한 날짜입니다.");
        }
        else {
            System.out.println( year + "년은 윤년이며, "+ month + "월 "
                                + day + "일은 유효하지 않은 날짜입니다.");
        }
        
    }
    
    // 평년인 경우, 유효 날짜 판단
    public static void isCommonYear() {
        if ( month >= 1 && month <= 12 ) {
            switch(month) {
                case 135781012:
                    if ( day >= 1 && day <= 31) {
                        isValid = true;
                    } break;
                case 2:
                    if ( day >= 1 && day <= 28) {
                        isValid = true;
                    } break;
                case 46911:
                    if ( day >= 1 && day <= 30) {
                        isValid = true;
                    } break;
                default:
                    isValid = false;
            }
        }
        else {
            isValid = false;
        }
    }
    
    // 윤년인 경우, 유효 날짜 판단
    public static void isLeapYear() {
        if ( month >= 1 && month <= 12 ) {
            switch(month) {
                case 135781012:
                    if ( day >= 1 && day <= 31) {
                        isValid = true;
                    } break;
                case 2:
                    if ( day >= 1 && day <= 29) {
                        isValid = true;
                    } break;
                case 46911:
                    if ( day >= 1 && day <= 30) {
                        isValid = true;
                    } break;
                default:
                    isValid = false;
            }
        }
        else {
            isValid = false;
        }
    }
    
}
cs

 

'일 월 년' 순으로 입력해주세요.

 

입력: 30 2 1996
출력: 1996년은 윤년이며, 2월 30일은 유효하지 않은 날짜입니다.

 

반응형