1 //输入年份与月份,打印出日历
2 //1900-1-1 星期一
3 public static void exercise15(){
4 Scanner input=new Scanner(System.in);
5 System.out.print("请输入年份:");
6 int year=input.nextInt();
7 System.out.print("请输入月份:");
8 int moth=input.nextInt();
9 //判断年份输入是否合法
10 if (year>1900) {
11 //判断月份输入是否合法
12 if (moth>12||moth<0) {
13 System.out.println("你输入的月份非法!");
14 exercise15();
15 }
16 else{
17 System.out.println("\t"+year+"年的"+moth+"月日历如下");
18 System.out.print("日\t一\t二\t三\t四\t五\t六\n");
19 System.out.println("********************************************************************");
20 int tnum=0;//保存输入月份前已经经历天数
21 for (int i=1900;i<year ;i++ ) {//输入年份的开始与1900-1-1经历多少天
22 if ((i%4==0 && i%100!=0)|| i%400==0) {
23 tnum=tnum+366;
24 }
25 else{
26 tnum=tnum+365;
27 }
28 }
29 for (int i=1;i<=moth-1 ;i++ ) {//到前一个月的天数
30 if (i==2) {
31 tnum+=28;
32 }
33 else if(i==4||i==6||i==9||i==11){
34 tnum+=30;
35 }
36 else tnum+=31;
37 }
38 int ye=0;
39 if(moth==4||moth==6||moth==9||moth==11){//判断输入月份天数
40 ye=30;
41 }else if(moth==2)
42 ye=28;
43 else ye=31;
44
45 //判断输入年是否为闰年
46 if ((year%4==0 && year%100!=0)|| year%400==0) {
47 if (moth>2) {//是闰年且输入月份大于2月,总天数+1
48 tnum++;
49 }
50
51 if(moth==2)//是闰年且输入月份为2月 月份天数+1
52 ye=29;
53 }
54 //计算输入月份星期起点
55 int xx=(tnum+1)%7;
56 for (int j=1;j<=xx ;j++ ) {//输出第一行的空格
57 System.out.print(" \t");
58 }
59 int j=1;
60 for (;j<=7-xx ;j++ ) {//输出第一行日历
61 System.out.print(j+" \t");
62 }
63
64 for (;j<=ye ;j++ ) {//输出其他行日历
65 if ((j+xx-1)%7==0) {
66 System.out.print(" \n");
67 }
68 System.out.print(j+" \t");
69 }
70
71 }
72 }else{
73 System.out.println("你输入的年份非法!");
74 exercise15();
75 }
76 }
1 public static void calendar(){//程序起头
2 Scanner input=new Scanner(System.in);
3 System.out.print("请输入年份");
4 int year=input.nextInt();
5 System.out.print("请输入月份");
6 int month=input.nextInt();
7 //判断输入值
8 if (year<1900||(month>12&&month<1)) {
9 System.out.print("输入的年份或月份有误!请重试:");
10 calendar();
11 }else{
12 //获取1号星期数
13 int weekNum=(calendarYearDayeNum( year)+calendarMonthDayeNum(year,month))%7;
14 calendarDisplay( weekNum,month, year);
15 System.out.println("输入eixt结束本程序!输入其他继续使用!");
16 System.out.print("请输入你的选项:");
17 String continu=input.next();
18 if ("eixt".equals("continu")) {
19 return;
20 }
21 calendar();
22 }
23 }
24
25 //判断年份是否为闰年
26 public static boolean calendarLeapYear(int year){
27 //判断year的合法性
28 boolean flag=false;//初始化为不是闰年
29 if((year%4==0&&year%100!=0)||year%400==0)
30 flag=true;//是闰年返回true
31 return flag;
32 }
33
34 //根据年份和月份,输出这个月有多少天。
35 public static int calendarMonthNum(int year,int month){
36 int daysNum=0;
37 if(month==4||month==6||month==9||month==11)
38 daysNum=30;
39 else if (month==2) {
40 daysNum=28;
41 if (calendarLeapYear(year)) {//是闰年二月加一天
42 daysNum+=1;
43 }
44 }
45 else{//其余的是大月
46 daysNum=31;
47 }
48 return daysNum;
49 }
50
51 //3:根据年份,求这一年的1。1日离1900年1.1的天数
52 public static int calendarYearDayeNum(int year){
53 int daysSum=0;//记录天数
54 for (int i=1900;i<year ;i++ ) {
55 daysSum+=365;//默认每年为365天
56 if (calendarLeapYear(i)) {//是闰年加一天
57 daysSum+=1;
58 }
59 }
60 return daysSum;
61 }
62
63 //4:根据年份和月份,求这一年的这个月的1号离这一年的1.1号的天数。
64 public static int calendarMonthDayeNum(int year,int month){
65 int monthSum=0;//记录天数
66 for (int i=1;i<month ;i++ ) {
67 monthSum+=calendarMonthNum(year,i);//调用求月份天数方法
68 }
69 return monthSum;
70 }
71
72
//需求,根据1号的星期打印出这个月的日历
//num 星期数 小于7
public static void calendarDisplay(int num,int month,int year){
//打印头部
System.out.println("\t\t "+year+"年的"+month+"月");
System.out.println("------------------------------------------------------");
System.out.print("日\t一\t二\t三\t四\t五\t六\n");
//打印第一空格
num=num%7;
for (int i=1;i<=num ;i++ ) {
System.out.print("\t");
}
//打印日历
for(int i=1;i<calendarMonthNum( year,month);i++){
System.out.print(i+"\t");//输出日历
if ((i+num)%7==0) {
System.out.print("\n");//到周六后换行
}
}
System.out.print("\n");
}
请输入年份:2017
请输入月份:7
2017年的7月日历如下
日 一 二 三 四 五 六
********************************************************************
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31