输入年份,月份,打印出该月日历表。
:日历表从2000年起始。
package repeat_job1; public class Calendar { public Calendar(){} private int []a={366,365,365,365}; public int number_Days(int m,int n) { int num1=((m-2000)/4)*1461; int num2=0,sum_days=0; for(int i=0;i<(m-2000)%4;i++) { num2+=a[i]; } sum_days=num1+num2+number_Day(m,n); return sum_days; } public int leapyear(int m) { if((m%4==0&&m%100!=0)||m%400==0) { return 29; } else { return 28; } } public int number_Day(int m,int n) { int r=0; switch(n) { case 12:r+=30; case 11:r+=31; case 10:r+=30; case 9:r+=31; case 8:r+=31; case 7:r+=30; case 6:r+=31; case 5:r+=30; case 4:r+=31; case 3:r+=leapyear(m); case 2:r+=31; case 1:r+=0; } return r; } public void print(int m,int n) { System.out.println("星期日"+"\t"+"星期一"+"\t"+"星期二"+"\t"+"星期三"+"\t"+"星期四"+"\t"+"星期五"+"\t"+"星期六"); int mod,day; mod=(1+number_Days(m,n))%7;//为什么加1? switch(mod) { case 2:System.out.print("");break; case 3:System.out.print("");break; case 4:System.out.print("\t");break; case 5:System.out.print("\t\t");break; case 6:System.out.print("\t\t\t");break; case 0:System.out.print("\t\t\t\t");break; case 1:System.out.print("\t\t\t\t\t");break; } if(n==1||n==3||n==5||n==7||n==8||n==10||n==12) { for(day=1;day<=31;day++) { mod=(number_Days(m,n)+day)%7; switch(mod) { case 2:System.out.print(" "+day);break; case 3:System.out.print("\t"+day);break; case 4:System.out.print("\t"+day);break; case 5:System.out.print("\t"+day);break; case 6:System.out.print("\t"+day);break; case 0:System.out.print("\t"+day);break; case 1:System.out.println("\t"+day);break; } } } else if(n==2) { for(day=1;day<=leapyear(m);day++) { mod=(number_Days(m,n)+day)%7; switch(mod) { case 2:System.out.print(" "+day);break; case 3:System.out.print("\t"+day);break; case 4:System.out.print("\t"+day);break; case 5:System.out.print("\t"+day);break; case 6:System.out.print("\t"+day);break; case 0:System.out.print("\t"+day);break; case 1:System.out.println("\t"+day);break; } } } else{ for(day=1;day<=30;day++) { mod=(number_Days(m,n)+day)%7; switch(mod) { case 2:System.out.print(" "+day);break; case 3:System.out.print("\t"+day);break; case 4:System.out.print("\t"+day);break; case 5:System.out.print("\t"+day);break; case 6:System.out.print("\t"+day);break; case 0:System.out.print("\t"+day);break; case 1:System.out.println("\t"+day);break; } } } } }
import repeat_job1.Calendar; import java.util.Scanner; public class Test2 { public static void main(String[] args) { Calendar ca=new Calendar(); Scanner reader=new Scanner(System.in); System.out.println("请输入年份:"); int m=reader.nextInt(); System.out.println("请输入月份:"); int n=reader.nextInt(); reader.close(); ca.print(m,n); } }
时间: 2024-10-12 19:59:26