1. 입력 받은 년/월 의 달력을 출력.


 

//java MyCalendar 2011 1
public class MyCalendar{
 public static void main(String[] args) {
  int year = Integer.parseInt(args[0]);
  int month =Integer.parseInt(args[1]);
  int day = 1;
  int sum = 0;
  int maxDay = 0;
  System.out.println("일\t월\t화\t수\t목\t금\t토\t");
  for (int i=1; i<year ;i++ ){
   if (i%400 ==0 || (i%4==0&&i%100!=0)){
    sum+=366;
   }
   else{
    sum+=365;
   }
  }
  if(year%400 ==0 || (year%4==0&&year%100!=0)){
   int[] array = {31,29,31,30,31,30,31,31,30,31,30,31};
   for (int i=0 ; i<month-1 ; i++ ){
    sum+=array[i];
   }
  }
  else{//윤년이 아니면
   int[] array1 = {31,28,31,30,31,30,31,31,30,31,30,31};
   for (int i=0 ; i<month-1 ; i++ ){
    sum+=array1[i];
  }
  }
  sum++;
 // System.out.println("총 일수="+sum);
  int space = sum%7;
  // System.out.println("무슨 요일?="+space);
  switch(month){
   case 1: ;
   case 3: ;
   case 5: ;
   case 7: ;
   case 8: ;
   case 10: ;
   case 12: maxDay = 31; break;
   case 4 : ;
   case 6 : ;
   case 9 : ;
   case 11 :  maxDay = 31; break;
   case 2 : 
    if(year%400 ==0 || (year%4==0&&year%100!=0)){
    maxDay = 29;
   }
   else{
    maxDay = 28;
   }
  }
  int count = 0;
  // 공백부터출력
  for (int i =0 ; i  <space ; i++ ){
   System.out.print("★\t");  
   count++;
  }
  for (int i =1 ; i  <=maxDay ; i++ ){
   System.out.print(i+"\t");
   count++;
   if (count%7==0){
    System.out.println();
   }
  }  
  //밑단 별
  if(((space+maxDay)%7)!=0){
   for (int i =0 ; i  <7-((space+maxDay)%7) ; i++ ){
    System.out.print("☆\t"); 
   }
  }
 }
}
 코드 실행결과는 아래와 같다.

+ Recent posts