U E D R , A S I H C RSS

만년달력/영동

소감

2학년때 데블스캠프 때 못 풀다가 버그 생겨서 포기한 문제였는데... 얼마 전에 자바 숙제로 비슷하지만 좀 더 쉬운 문제가 나왔었는데, 그걸 풀고 나니 내가 그때 이걸 왜 못 풀었을까...하는 생각이 드는군요. 밑의 소스는 리팩토링 할 필요가 있긴 하지만요.
그런데 자바 책 다음장에 만년달력 구하는 소스가 또 있었군요. 이거 풀고 나서 알아버렸네요.ㅎㅎ

코드

import javax.swing.*;
public class EternalCalendar
{
	public static void main(String []args)
	{
		int[] daysOfMonth={31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};//각 달의 날짜수
		String[] nameOfday={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};//각 요일의 이름

		int dYear=Integer.parseInt(JOptionPane.showInputDialog(null, "알고 싶은 연도 입력:", "만년달력", JOptionPane.QUESTION_MESSAGE));
		int dMonth=Integer.parseInt(JOptionPane.showInputDialog(null, "알고 싶은 월 입력:", "만년달력", JOptionPane.QUESTION_MESSAGE));
		//알고 싶은 년/월을 입력받음

		int totalDays=0;//그 해까지의 총 날짜수

		for(int i=1;i<dYear;i++)
		{
			if((i%4==0 && i%100!=0) || i%400==0)
				totalDays+=366;
			else
				totalDays+=365;
		}//그 전해까지의 총 날짜수

		if((dYear%4==0 && dYear%100!=0) || dYear%400==0)
			daysOfMonth[1]=29;//입력받은 해의 2월에 윤달이 있는지 계산
		for(int i=1;i<dMonth;i++)//해당하는 해의 그 전달까지의 날짜수
			totalDays+=daysOfMonth[i-1];

		System.out.println(dYear+"년 "+dMonth+"월의 달력");
		for(int i=0;i<7;i++)//달력의 상단에 요일 출력
			System.out.print(nameOfday[i]+"\t");
		System.out.print("\n");
		for(int i=0;i<(totalDays+1)%7;i++)
			System.out.print("\t");//해당 달의 첫날의 요일 설정. +1인 이유는 1년 1월 1일은 일요일
		for(int i=1;i<daysOfMonth[dMonth-1]+1;i++)
		{//1일부터 마지막 날짜까지 찍음
			System.out.print(i+"\t");
			if((i+totalDays+1)%7==0)//한 줄에 7개만 들어가도록 줄바꿈(최초의 빈칸도 계산)
				System.out.print("\n");
		}
		System.out.print("\n");
	}
}
----
작성자: 임영동 Yggdrasil
----
만년달력
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:26
Processing time 0.0141 sec