영보의 SystemOut.log

[JAVA]달력프로그램 만들기/자바배열예제/자바배열 본문

Language/JAVA

[JAVA]달력프로그램 만들기/자바배열예제/자바배열

영보로그 2020. 7. 2. 16:37
반응형
* 주의 
2020
   → 전년도까지의 총날 수 1년~2019.12.31
7
   →전달까지의 합 2020.1.1+2020.6.30
      +1
 

* 소스 코드

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
import java.util.*;
public class 달력만들기{
   public static void main(String[] args){
       
       int year=0, month=0;
       Scanner scan=new Scanner(System.in);
       System.out.println("년도 입력: ");
       year=scan.nextInt();
       
       System.out.println("월 입력: ");
       month=scan.nextInt();
       
       //출력
       String[] strWeek= {"일","월","화","수","목","금","토"};
       System.out.println(year+"년도 "+month+"월");
       
       for(String week:strWeek)
       {
           System.out.print(week+"\t");
       }
       
       int total=(year-1)*365
                +(year-1)/4
                -(year-1)/100
                +(year-1)/400;
       
       //전달
       int[] lastDay= {31,28,31,30,31,30,31,31,30,31,30,31};
       if((year%4==0 && year%100!=0)||(year%400==0)) //윤년 => 2월 29일
           lastDay[1]=29;
       else
           lastDay[1]=28;
       for(int i=0;i<month-1;i++)
       {
           total+=lastDay[i];
       }
       //1일자의 요일
       total++;
       
       int week=total%7;
       //달력 출력
       System.out.println();
       for(int i=1;i<=lastDay[month-1];i++)
       {
           if(i==1)
           {
              for(int j=0;j<week;j++)
              {
               System.out.print("\t");
              }
           }
           System.out.printf("%2d\t",i);
           week++;
           if(week>6)
           {
               week=0;
               System.out.println();
               }
       }
   }
}
cs

 

 

 

* 결과 화면

 

반응형