解题思路:
1.找到这一年一月份是从星期几开始,计算方法求365或366的余数即可
2.计算这个月之前有多少天,计算出这个月从星期几开始,
3.输出记得到了星期6换行,最后一行不换行
注意事项:
参考代码:
import java.util.Scanner; public class 任意年月日历输出 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int year=in.nextInt(); int month=in.nextInt(); int y=2007;int m=1;int week=1; while(y<year){ if((y%4==0&&y%100!=0)||y%400==0)//闰年366天 {week=week+366%7;}//下一年一月份开始从星期week开始。 else week=week+365%7; y++; } System.out.println("---------------------"); System.out.println(" Su Mo Tu We Th Fr Sa"); System.out.println("---------------------"); //计算这个月之前有多少天 int temp=0; while(m<month){ if(m==1)temp=31; if(m==2){ if(((y%4==0&&y%100!=0)||y%400==0)) temp+=29; else temp+=28; } if(m==3)temp+=31; if(m==4)temp+=30; if(m==5)temp+=31; if(m==6)temp+=30; if(m==7)temp+=31; if(m==8)temp+=31; if(m==9)temp+=30; if(m==10)temp+=31; if(m==11)temp+=30; if(m==12)temp+=31; m++; } week=(week+temp%7)%7;//计算这个月第一天星期数 int day=1;temp=-1;//因为第一个输出是星期日 if(((y%4==0&&y%100!=0)||y%400==0)&&m==2){//当这年是闰年且是二月份 while(++temp<week){System.out.print(" ");} System.out.printf("%3d",day);week++; while(++day<=29){ System.out.printf("%3d",day); if(week==6&&day!=29)System.out.println(); week++;week=week%7; } } else if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){ while(++temp<week){System.out.print(" ");} System.out.printf("%3d",day);week++; while(++day<=31){ System.out.printf("%3d",day); if(week==6&&day!=31)System.out.println(); week++;week=week%7; } } else if(m==4||m==6||m==9||m==11){ while(++temp<week){System.out.print(" ");} System.out.printf("%3d",day);week++; while(++day<=30){ System.out.printf("%3d",day); if(week==6&&day!=30)System.out.println(); week++;week=week%7; } } else{ while(++temp<week){System.out.print(" ");} System.out.printf("%3d",day);week++; while(++day<=28){ System.out.printf("%3d",day); if(week==6&&day!=28)System.out.println(); week++;week=week%7; } } System.out.println(); System.out.print("---------------------"); } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:668 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:1071 |
字符串问题 (C语言代码)浏览:1634 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:541 |
WU-整除问题 (C++代码)浏览:648 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:564 |
三角形 (C++代码)递推浏览:825 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:628 |
C语言考试练习题_保留字母 (C语言代码)浏览:743 |
C语言程序设计教程(第三版)课后习题9.1 (C语言代码)浏览:710 |