来,咱们今天继续研究上学期作业。
这个作业3其实有点惊到我了,当时我感觉也没太认真就做完了,回头一看发现……好长!而且要求好多!
其实就是一个写日历的简单程序,但说实话人类的公历纪年法奇怪的要求确实有点多,不过还是做完了。
代码如下:
package natusyasumi; import java.util.Scanner; class Date { private int year=1,month=1,day=1; public Date(int a,int b,int c) { year=a; month=b; day=c; }; public Date(){} public void setYear(int a) { year=a; } public void setMonth(int b) { month=b; } public void setDay(int c) { day=c; } public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public void yesterday( ) { int s=99; day--; s=isLeapYear( ); if(month==1||month==4||month==6||month==8||month==9||month==11) { if(day<1) { month--; if (month<1) { month=12+month; year--; } day=31+day; } } else if(month==3) { if(s==1) { if(day<1) { month--; day=29+day; } } else { if (day<1) { month--; day=28-day; } } } else { if(day<1) { month--; day=30+day; } } } public void tomorrow( ) { int s; day++; s=isLeapYear( ); if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) { if(day>31) { month++; if (month>12) { month=month-12; year++; } day=day-31; } } else if(month==2) { if(s==1) { if(day>29) { month++; day=day-29; } } else { if (day>28) { month++; day=day-28; } } } else { if(day>30) { month++; day=day-30; } } } public int isLeapYear( ) { int x=99; if(year%400==0) { x=1; } else if(year%100==0) { x=0; } else if(year%4==0) { x=1; } return x; } public void chineseFormat( ) { System.out.print(year+"年"+month+"月"+day+"日"); } public void americaformat( ) { switch(month) { case 1:System.out.print("Jan");break; case 2:System.out.print("Fed");break; case 3:System.out.print("Mar");break; case 4:System.out.print("Apr");break; case 5:System.out.print("May");break; case 6:System.out.print("Jun");break; case 7:System.out.print("Jul");break; case 8:System.out.print("Aug");break; case 9:System.out.print("sep");break; case 10:System.out.print("Oct");break; case 11:System.out.print("Nov");break; case 12:System.out.print("Dec");break; } System.out.print(" "+day+","+year+"\n"); } public void printMonthCalendar( ) { int w=getdate(); int[] monthed={31,28,31,30,31,30,31,31,30,31,30,31}; if(isLeapYear( )==1) monthed[1]=29; System.out.print("SunMonTueWedThuFriSat\n"); int i,j; for(i=0;i<w;i++) System.out.print(" "); for(i=w,j=1;j<=monthed[month-1];i++,j++) { if(i%7==0) System.out.print("\n"); if(j<10) System.out.print(" "+j+" "); else System.out.print(" "+j); } System.out.print("\n"); } public int getdate() { int w=0; w=(year+(year-1)/4-(year-1)/100+(year-1)/400)%7; int days=0; switch(month) { case 12: days+=30; case 11: days+=31; case 10: days+=30; case 9: days+=31; case 8: days+=31; case 7: days+=30; case 6: days+=31; case 5: days+=30; case 4: days+=31; case 3: if(isLeapYear( )==1) days+=29; else days+=28; case 2: days+=31; case 1: days+=0; } w=(w+days)%7; return w; } } final class Class3 { public static void main(String[] args) { int year,month,day; Date d1=new Date(); Date d2=new Date(); Scanner input=new Scanner(System.in); year=input.nextInt(); month=input.nextInt(); day=input.nextInt(); d1.setYear(year); d1.setMonth(month); d1.setDay(day); d1.yesterday(); d1.chineseFormat(); year=input.nextInt(); month=input.nextInt(); day=input.nextInt(); input.close(); d2.setYear(year); d2.setMonth(month); d2.setDay(day); d2.tomorrow(); d2.americaformat(); d2.printMonthCalendar(); } }
运行结果如下: