본문 바로가기

Java

년,월,일을 입력받아 요일을 출력해주는 프로그램

package com.day6;
import java.util.Scanner;

public class Test1 {

	public static void main(String[] args) {
		
		Scanner sc= new Scanner(System.in);
		
		// 월별 날수를 배열에 저장
		int months[] = {31,28,31,30,31,30,31,31,30,31,30,31};
		
		int y,m,nalsu,i,week,d;
		String str[]= {"일","월","화","수","목","금","토"};
		
		do {
			System.out.print("년도?");
			y=sc.nextInt();
		}while(y<1); 
		
		do {
			System.out.print("월?");
			m=sc.nextInt();
		}while(m<1&&m>12);
		
		//윤년 먼저 계산
		if (y%4 == 0 && y%100 != 0 || y%400 ==0)
			months[1]=29;
		
		do {
			System.out.print("일?"); 
			d=sc.nextInt();
		}while(d<1&&d>months[m-1]);
		
		
		//1년 1월1일부터 ~ 현재년의 전년도 12월 31일까지의 날수 
		//+ 2018년까지의 윤년 수
		nalsu=(y-1)*365 + (y-1)/4 - (y-1)/100 + (y-1)/400; 
		
		for(i=0;i<m-1;i++) {
			nalsu+=months[i]; 
		}
		
		nalsu += d; // d일 까지의 날수
		
		//주의 수 (0 1 2 3 4 5 6)
		week= nalsu%7; //1일때는 월요일 ,2일때는 화요일

		System.out.printf("%d년 %d월 %d일은 %s 입니다",y,m,d,str[week]);
	}

}