码迷,mamicode.com
首页 > 其他好文 > 详细

万年历

时间:2017-09-25 14:30:54      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:class   ring   ann   sys   key   int   代码段   []   amp   

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);
  }

}

万年历

标签:class   ring   ann   sys   key   int   代码段   []   amp   

原文地址:http://www.cnblogs.com/hxtblogs/p/7591325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!