JAVA 프로그래밍

문제

생일과 오늘을 입력하면 태어난 지 몇 일째인지 출력하는 알고리즘입니다 이를 해결하는 프로그램을 작성하세요 
생년을 입력하세요: 1397
생월을 입력하세요: 5
생일을 입력하세요: 15
올해를 입력하세요: 1450
이번 달을 입력하세요: 3
오늘 날짜를 입력하세요: 30
1397년 5월 15일부터 1450년 3월 30일까지 19312일째 잘 견디고 잘 살아내셨어요. 당신을 응원합니다. 

생년을 입력하세요: 1752
생월을 입력하세요: 10
생일을 입력하세요: 28
올해를 입력하세요: 1800
이번 달을 입력하세요: 8
오늘 날짜를 입력하세요: 18
1752년 10월 28일부터 1800년 8월 18일까지 17461일째 잘 견디고 잘 살아내셨어요. 당신을 응원합니다. 

생년을 입력하세요: 1457
생월을 입력하세요: 8
생일을 입력하세요: 28
올해를 입력하세요: 1495
이번 달을 입력하세요: 1
오늘 날짜를 입력하세요: 29
1457년 8월 28일부터 1495년 1월 29일까지 13669일째 잘 견디고 잘 살아내셨어요. 당신을 응원합니다. 

알고리즘 설계

프로그램 시작 
   생일 및 오늘 날짜를 입력
   먼저 1년 1월 1일부터 생일 날짜까지 전체 날짜 계산
   1년 1월 1일부터 오늘까지 전체 날짜 계산
      1년 1월 1일부터 작년 12월 31일까지 날짜 계산
      올해 1월 1일부터 지난달 말일까지 날짜 계산
         올해가 윤년이면 2월 29일을 반영
      이번 달 1일부터 오늘까지 날짜 계산
   몇 일째 살아왔는지 계산 후 출력
프로그램 종료

※ 1년 1월 1일부터 오늘까지 전체 날짜 계산하는 방법
  1년 1월 1일 ~ 작년 12월 31일     올해 1월 1일 ~ 지난달 말일     이번 달 1일 ~ 오늘  

프로그램 구현

     
 
 ※ 단계별 점검: A키(이전), D키(다음) 
 
	// 파일명 : ./Chapter07/LivingPeriod.java
	import java.util.Scanner;
		  
	public class LivingPeriod
	{
		// 프로그램 시작 
1		public static void main( String[] args ) {
			Scanner scan = new Scanner( System.in );
			// 생일 및 오늘 날짜를 입력 
			System.out.print( "생년을 입력하세요: " );
			int birthYear = scan.nextInt();
			System.out.print( "생월을 입력하세요: " );
			int birthMonth = scan.nextInt();
			System.out.print( "생일을 입력하세요: " );
			int birthDay = scan.nextInt();
			System.out.print( "올해를 입력하세요: " );
			int thisYear = scan.nextInt();
			System.out.print( "이번 달을 입력하세요: " );
			int thisMonth = scan.nextInt();
			System.out.print( "오늘 날짜를 입력하세요: " );
2			int thisDay = scan.nextInt();
		  
			// 먼저 1년 1월 1일부터 생일 날짜까지 전체 날짜 계산 
			// 1년 1월 1일부터 직전 연도 12월31일까지 날짜 계산
3			int birthTotalDays = ( birthYear - 1 ) * 365 + ( birthYear - 1 ) / 4 - ( birthYear - 1 ) / 100 + ( birthYear - 1 ) / 400;
			// 해당 연도 1월 1일부터 직전 달 말일까지 날짜 계산
4			switch ( birthMonth - 1 ) {
5				case 12 :
6					birthTotalDays += 31;
7				case 11 :
8					birthTotalDays += 30;
9				case 10 :
10					birthTotalDays += 31;
11				case 9 :
12					birthTotalDays += 30;
13				case 8 :
14					birthTotalDays += 31;
15				case 7 :
16					birthTotalDays += 31;
17				case 6 :
18					birthTotalDays += 30;
19				case 5 :
20					birthTotalDays += 31;
21				case 4 :
22					birthTotalDays += 30;
23				case 3 :
24					birthTotalDays += 31;
25				case 2 :
26					birthTotalDays += 28;
					// 해당 연도가 윤년이면 2월 29일을 반영
27					if ( ( ( birthYear % 400 ) == 0 ) || ( ( ( birthYear % 100 ) != 0 ) && ( ( birthYear % 4 ) == 0 ) ) ) {
28						birthTotalDays++;
					}
29				case 1 :
30					birthTotalDays += 31;
			}
			// 해당달 1일부터 해당일까지 날짜 계산
31			birthTotalDays += birthDay;
		  
			// 1년 1월 1일부터 오늘까지 전체 날짜 계산 
			// 1년 1월 1일부터 작년 12월 31일까지 날짜 계산 
32			int thisTotalDays = ( thisYear - 1 ) * 365 + ( thisYear - 1 ) / 4 - ( thisYear - 1 ) / 100 + ( thisYear - 1 ) / 400;
			// 올해 1월 1일부터 지난달 말일까지 날짜 계산 
33			switch ( thisMonth - 1 ) {
34				case 12 :
35					thisTotalDays += 31;
36				case 11 :
37					thisTotalDays += 30;
38				case 10 :
39					thisTotalDays += 31;
40				case 9 :
41					thisTotalDays += 30;
42				case 8 :
43					thisTotalDays += 31;
44				case 7 :
45					thisTotalDays += 31;
46				case 6 :
47					thisTotalDays += 30;
48				case 5 :
49					thisTotalDays += 31;
50				case 4 :
51					thisTotalDays += 30;
52				case 3 :
53					thisTotalDays += 31;
54				case 2 :
55					thisTotalDays += 28;
					// 올해가 윤년이면 2월 29일을 반영 
56					if ( ( ( thisYear % 400 ) == 0 ) || ( ( ( thisYear % 100 ) != 0 ) && ( ( thisYear % 4 ) == 0 ) ) ) {
57						thisTotalDays++;
					}
58				case 1 :
59					thisTotalDays += 31;
			}
			// 이번 달 1일부터 오늘까지 날짜 계산 
60			thisTotalDays += thisDay;
		  
			// 몇 일째 살아왔는지 계산 후 출력 
61			int duration = thisTotalDays - birthTotalDays + 1;
62			System.out.print( birthYear + "년 " + birthMonth + "월 " + birthDay + "일부터 " + thisYear + "년 " + thisMonth + "월 " + thisDay + "일까지 " + duration + "일째 잘 견디고 잘 살아내셨어요. 당신을 응원합니다." );
			scan.close();
		// 프로그램 종료 
63		}
	}

실행 순서 확인