公历还是很简单的
1 import java.util.*;
2 class CalendarTest
3 {
4 /*先輸出提示語句,并接受用戶輸入的年、月。
5 根據(jù)用戶輸入的年,先判斷是否是閏年。
6 根據(jù)用戶輸入的年份來判斷月的天數(shù)。
7 用循環(huán)計(jì)算用戶輸入的年份距1900年1月1日的總天數(shù)。
8 用循環(huán)計(jì)算用戶輸入的月份距輸入的年份的1月1日共有多少天。
9 相加計(jì)算天數(shù),得到總天數(shù)。
10 用總天數(shù)來計(jì)算輸入月的第一天的星期數(shù)。
11
12 根據(jù)上值,格式化輸出這個(gè)月的日歷。*/
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 //月的天數(shù)。
29
30 System.out.println(year+"年"+month+"月有"+monthDayNum(month,comLeap)+"天");
31
32 //1900年到輸入年總天數(shù)。
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īng)"+j+"天");
41
42
43 //計(jì)算輸入的月份距輸入的年份的1月1日共有多少天。
44 int mDayNum=0;
45 int a=0;
46 for (int month1=1;month1<= month ;month1++ )//1月1日到輸入月1日天數(shù)
47 {
48 mDayNum+=a;
49 a=monthDayNum(month1,comLeap);
50
51 //累加月天數(shù)
52 }
53 System.out.println(year+"年到"+month+"月有"+mDayNum+"天");
54
55 //相加計(jì)算天數(shù),得到總天數(shù)。
56 int zDay=j+mDayNum;
57 System.out.println(year+"年"+month+"月距1900年1月1日已經(jīng)"+zDay+"天");
58
59 //用總天數(shù)來計(jì)算輸入月的第一天的星期數(shù)。
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);
//根據(jù)上值,格式化輸出這個(gè)月的日歷。 65 66 System.out.println("星期日 星期一 星期二 星期三 星期四 星期五 星期六"); 67 68 int hh=0;//記錄換行的地點(diǎn) 69 for (int sp=1;sp<=starDay ;sp++)//需要空出位置打印對應(yīng)星期的日期 70 { 71 System.out.print(" "+"\t"); 72 hh++; 73 } 74 75 for (int l=1;l<=monthDayNum(month,comLeap) ;l++)//打印每月天數(shù) 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 //根據(jù)輸入的年份來判斷月的天數(shù)。 99 static int monthDayNum(int month,boolean comLeap) 100 { 101 int dayNum; 102 if (month>=8){dayNum= month%2==0?31:30;}//月份大于八月且奇數(shù)是30天 103 104 else if (month==2){dayNum= comLeap ?29:28;}//2月 用閏年返回值來 賦值天數(shù) 105 106 else dayNum=month%2!=0?31:30;//小于七月奇數(shù)是31天 107 108 return dayNum; 109 } 110 111 112 }
//根據(jù)上值,格式化輸出這個(gè)月的日歷。 65 66 System.out.println("星期日 星期一 星期二 星期三 星期四 星期五 星期六"); 67 68 int hh=0;//記錄換行的地點(diǎn) 69 for (int sp=1;sp<=starDay ;sp++)//需要空出位置打印對應(yīng)星期的日期 70 { 71 System.out.print(" "+"\t"); 72 hh++; 73 } 74 75 for (int l=1;l<=monthDayNum(month,comLeap) ;l++)//打印每月天數(shù) 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 //根據(jù)輸入的年份來判斷月的天數(shù)。 99 static int monthDayNum(int month,boolean comLeap) 100 { 101 int dayNum; 102 if (month>=8){dayNum= month%2==0?31:30;}//月份大于八月且奇數(shù)是30天 103 104 else if (month==2){dayNum= comLeap ?29:28;}//2月 用閏年返回值來 賦值天數(shù) 105 106 else dayNum=month%2!=0?31:30;//小于七月奇數(shù)是31天 107 108 return dayNum; 109 } 110 111 112 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/coldDog/p/6395400.html
總結(jié)
- 上一篇: 如何利用脚本方法清除文本框中汉字,从而保
- 下一篇: Eclipse及Android环境配置