1 package learnExercise; 2 3 import java.util.Scanner; 4 5 public class PrintCalender { 6 7 /** 8 * @param args 9 */ 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 Scanner input=new Scanner(System.in); 13 14 System.out.print("Enter full year (e.g.,2001): "); 15 int year=input.nextInt(); 16 17 System.out.print("Enter month as number between 1 and 12: "); 18 int month=input.nextInt(); 19 20 printMonth(year,month); 21 22 } 23 24 private static void printMonth(int year, int month) { 25 // TODO Auto-generated method stub 26 printMonthTitle(year, month); 27 printMonthBody(year, month); 28 } 29 30 public static void printMonthTitle(int year, int month){ 31 System.out.println(" "+getMonthName(month)+" "+year); 32 System.out.println("-------------------------"); 33 System.out.println(" Sun Mont Tue Wed Thu Fri Sat"); 34 35 } 36 37 //打印月份主体 38 public static void printMonthBody(int year,int month){ 39 int startDay=getStartDay(year, month); 40 int numberOfDaysInMonth=getNumberOfDaysInMonth(year, month); 41 for(int i=0;i<startDay;i++) 42 System.out.print(" "); 43 for(int i=1;i<=numberOfDaysInMonth;i++){ 44 System.out.printf("%4d",i); 45 if((i+startDay)%7==0) 46 System.out.println(); 47 } 48 System.out.println(); 49 } 50 51 public static String getMonthName(int month){ 52 String monthName=""; 53 switch(month){ 54 case 1: monthName="January";break; 55 case 2: monthName="February";break; 56 case 3: monthName="March";break; 57 case 4: monthName="April";break; 58 case 5: monthName="May";break; 59 case 6: monthName="June";break; 60 case 7: monthName="July";break; 61 case 8: monthName="August";break; 62 case 9: monthName="September";break; 63 case 10: monthName="October";break; 64 case 11: monthName="Nobember";break; 65 case 12: monthName="December";break; 66 } 67 return monthName; 68 } 69 //获取指定月份的第一天是周几 70 public static int getStartDay(int year, int month){ 71 final int START_DAY_FOR_JAN_1_1800=8; 72 int totalNumberOfDays=getTotalNumberOfDays(year,month); 73 74 return (totalNumberOfDays+START_DAY_FOR_JAN_1_1800)%7; 75 } 76 //返回总共的天数,从1800年1月1日开始计算,直到指定月份1日的前一天 77 public static int getTotalNumberOfDays(int year,int month){ 78 int total=0; 79 for(int i=1800;i<year;i++) 80 if(isLeapYear(i)) 81 total=total+366; 82 else 83 total=total+365; 84 85 for(int i=1;i<month;i++) 86 total=total+getNumberOfDaysInMonth(year,i); 87 88 return total; 89 } 90 91 //返回指定月份的天数 92 public static int getNumberOfDaysInMonth(int year,int month){ 93 int result=0; 94 switch (month){ 95 case 1: 96 case 3: 97 case 5: 98 case 7: 99 case 8: 100 case 10: 101 case 12: 102 result=31; 103 break; 104 case 4: 105 case 6: 106 case 9: 107 case 11: 108 result=30; 109 break; 110 case 2: 111 result=isLeapYear(year)?29:28; 112 break; 113 } 114 return result; 115 } 116 117 //判断是否为闰年 118 public static boolean isLeapYear(int year){ 119 return (year%400==0)||(year%4==0&&year%100!=0); 120 } 121 122 }
方法抽象将程序模块化为整齐、层次分明的形式。将程序由简洁的方法构成的集合,比其他方式更容易编写、调试、维护和修改。
编写大型程序时,可以使用自顶向下或自底向上的方法,不要一次性地编写整个程序。