公历还是很简单的

  1 import java.util.*;
  2 class  CalendarTest
  3 {
  4     /*先输出提示语句,并接受用户输入的年、月。
  5     根据用户输入的年,先判断是否是闰年。
  6     根据用户输入的年份来判断月的天数。
  7     用循环计算用户输入的年份距1900年1月1日的总天数。
  8     用循环计算用户输入的月份距输入的年份的1月1日共有多少天。
  9     相加计算天数,得到总天数。
 10     用总天数来计算输入月的第一天的星期数。
 11 
 12     根据上值,格式化输出这个月的日历。*/
 13     public static void main(String[] args) 
 14     {
 15 //        System.out.println("Hello World!");
 16 
 17         Scanner sc= new Scanner(System.in);
 18 //*****************************************
 19                                 //先输出提示输入年、月。
 20         System.out.print("输入年份:");
 21             int year=sc.nextInt();
 22         System.out.print("输入月份:");
 23             int month=sc.nextInt();
 24 //*****************************************
 25                                 //是否是闰年。
 26         boolean comLeap=isLeapYear(year);
 27 //*****************************************
 28                                 //月的天数。
 29 
 30 System.out.println(year+"年"+month+"月有"+monthDayNum(month,comLeap)+"天");
 31 
 32                                 //1900年到输入年总天数。
 33     int i=1900 ,j=0;
 34         while (i<year)
 35         {
 36         j+= isLeapYear(i)?366:365;
 37         i++;
 38         }
 39         
 40         System.out.println(year+"年距1900年1月1日已经"+j+"天");
 41 
 42 
 43                                 //计算输入的月份距输入的年份的1月1日共有多少天。
 44 int mDayNum=0;
 45 int a=0;
 46     for (int month1=1;month1<= month ;month1++ )//1月1日到输入月1日天数
 47     { 
 48         mDayNum+=a;
 49         a=monthDayNum(month1,comLeap);
 50         
 51         //累加月天数
 52     }
 53 System.out.println(year+"年到"+month+"月有"+mDayNum+"天");
 54 
 55                                 //相加计算天数,得到总天数。
 56 int zDay=j+mDayNum;
 57 System.out.println(year+"年"+month+"月距1900年1月1日已经"+zDay+"天");
 58 
 59                                 //用总天数来计算输入月的第一天的星期数。
 60 int starDay;
 61 /*if (zDay<1){starDay=1;}
 62 else{starDay=(zDay%7)+1;}*/
 63 starDay=zDay<1 ?1:(zDay%7)+1;
 64 System.out.println("今天是星期"+starDay);                                
                     //根据上值,格式化输出这个月的日历。 65 66 System.out.println("星期日 星期一 星期二 星期三 星期四 星期五 星期六"); 67 68 int hh=0;//记录换行的地点 69 for (int sp=1;sp<=starDay ;sp++)//需要空出位置打印对应星期的日期 70 { 71 System.out.print(" "+"\t"); 72 hh++; 73 } 74 75 for (int l=1;l<=monthDayNum(month,comLeap) ;l++)//打印每月天数 76 { 77 78 System.out.print(" "+l+"\t"); 79 hh++; 80 while (hh==7) 81 {System.out.println(); 82 hh=0; 83 } 84 } 85 } 86 87 //***************************************** 88 //判断是否是闰年。 89 static boolean isLeapYear(int year) 90 { 91 if ((year%4==0&& year%100!=0)||(year%400==0)) 92 {return true; 93 } 94 return false; 95 96 } 97 //***************************************** 98 //根据输入的年份来判断月的天数。 99 static int monthDayNum(int month,boolean comLeap) 100 { 101 int dayNum; 102 if (month>=8){dayNum= month%2==0?31:30;}//月份大于八月且奇数是30天 103 104 else if (month==2){dayNum= comLeap ?29:28;}//2月 用闰年返回值来 赋值天数 105 106 else dayNum=month%2!=0?31:30;//小于七月奇数是31天 107 108 return dayNum; 109 } 110 111 112 }

 

posted @ 2017-02-13 21:11  菊花缝纫师  阅读(344)  评论(0编辑  收藏  举报