package pro2;
import java.util.Scanner;
public class Year01 {
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.print("请输入要打印的年月(格式如201709)");
    int flag=sc.nextInt();
    test(flag);
  }
  public static int year(int year){
    if((year%400==0)||(year%4==0&&year%100!=0)){
      return 1;
    }
    else
      return 0;
  }
  public static int month(int year,int month){
    int one[]={1,3,5,7,8,10,12,-1,-1,-1,-1,-1};
    int two[]={4,6,9,11,-1,-1,-1,-1,-1,-1,-1,-1};
    for(int i=0;i<12;i++){
      if(month==one[i]){
        return 31;
      }
      else if(month==two[i]){
        return 30;
      }
      else if(month==2){
        int key=year(year);
        if(key==1){
          return 29;
        }
        else if(key==0){
          return 28;
        }
      }
    }
    return 0;
  }
  public static int judgeDay(int year,int month){
    int Ycount=0;
    int Mcount=0;
    {//此代码段判断某年前一共差多少天
      for(int i=1900;i<year;i++){
        int key1=year(i);
        if(key1==1){
          Ycount+=366;
        }else if(key1==0){
          Ycount+=365;
        }
       }
    }{//此代码段判断 某月前一共多少天
      for(int i=1;i<month;i++){
        int key2=month(year,i);
        Mcount+=key2;
      }
    }
    return (Ycount+Mcount);
  }
  public static void printMonth(int year,int month){
    int key=judgeDay(year,month);
    int week=(key+1)%7;
    System.out.println("Sun Mon Tue Wed Thu Fri Sat");
    for(int i=0;i<week;i++){
      System.out.printf(" ");
    }
    for(int i=0;i<month(year,month);i++){
      System.out.printf(" %-3d",i+1);
      if((week+i)%7==6){
        System.out.print("\n");
      }
    }
  }
  public static void test(int num){
    int year=num/100;
    int month=num%100;
    printMonth(year,month);
  }

}

posted on 2017-09-25 12:39  煮咖啡的猪!  阅读(163)  评论(0编辑  收藏  举报