列印日曆並呼叫多種方法的Java例項子
阿新 • • 發佈:2018-12-25
import java .util.Scanner; public class 列印日曆 { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.println("Enter full year(eg. 2012): "); int year=input.nextInt(); //提示使用者輸入月份 System.out.println("Enter month as a number between 1 and 12: "); int month=input.nextInt();//以上均為正常輸入,年份和月份 printMonth(year,month);//呼叫第一個方法,也是總的方法 } public static void printMonth(int year, int month) {//總的方法,列印日曆,被呼叫 // 列印日曆頭 printMonthTitle(year,month); //列印日曆主體 printMonthBody(year,month);//可以分為兩部,第一部分為列印標題,第二部分,列印主體 } public static void printMonthTitle(int year, int month) {//先列印標題,呼叫了一個方法,“得到月份的英文名” System.out.println(" "+getMonthName(month)+" "+year); System.out.println(" Sun Mon Tue Wed Thu Fri Sat "); } public static void printMonthBody(int year, int month) {//列印主體,呼叫了兩個方法 //Get start day of the week for the first date in the month int startDay=getStartDay(year,month);//得到該年該月的第一天的星期幾? int numberOfDaysInMonth=getNumberOfDaysInMonth(year,month);//得到該月的天數,然後在下面打印出來 // pad space before the first day of the month int i=0; for(i=0;i<startDay;i++)//根據得到的星期幾而控制第一排空格的數量 System.out.print(" "); for(i=1;i<=numberOfDaysInMonth;i++) {//列印天數 System.out.printf("%6d",i); if((i+startDay)%7==0) //如果說第i天和第一天的距離為7的倍數,則跳轉到下一行,比如startDay是5-Fri,那麼當i=2,跳轉到第二行。 System.out.println(); } System.out.println();//只是在所有的都列印完了之後再空一行 } // get the English name for the month public static String getMonthName(int month) {//不呼叫任何方法,只是把數字和英文月數連線起來。 String monthName=" "; switch(month) { case 1: monthName="January";break; case 2:monthName="Feburary";break; case 3:monthName="March"; break; case 4:monthName="April";break; case 5:monthName="May";break; case 6:monthName="June";break; case 7:monthName="July";break; case 8:monthName="August";break; case 9:monthName="Spetember";break; case 10:monthName="October";break; case 11:monthName="November";break; case 12:monthName="December";break; } return monthName; } public static int getStartDay(int year,int month) {//呼叫一個方法-----得到輸入的年月到1800年1.1的天數 final int START_DAY_FOR_JAN_1_1800=3;//1800年的1月1號是星期3 //Get total number of days from 1/1/1800 to month/1/year int totalNumberOfDays=getTotalNumberOfDays(year,month); //return the start day for month/1/year return (totalNumberOfDays+START_DAY_FOR_JAN_1_1800)%7;//通過把總天數和1800年1.1的日期數加起來再求7的餘數就可以了。 } public static int getTotalNumberOfDays(int year, int month) {//呼叫一個方法,getNumberOfDaysInMonth //Get the total number of days since January 1, 1800 int total=0; //Get the total days from 1800 to 1/1/year for(int i=1800;i<year;i++) if(isLeapYear(i)) total=total+366; else total=total+365;//加上之前所有年的天數 //Add days from January to the month prior to the current month for(int i=1;i<month;i++) // total=total+getNumberOfDaysInMonth(year,i);//呼叫方法,得到本年度所有月份中的天數 return total; } public static int getNumberOfDaysInMonth(int year, int month) { if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) return 31; if(month==4||month==6||month==9||month==11) return 30; if(month==2) return isLeapYear(year)? 29:28; return 0;//if month is incorrect } public static boolean isLeapYear(int year) {//決定是不是閏年 //determine if it is a leapYear return year%400==0||(year%4==0&&year%100!=0); } }
//注意一共呼叫了8個方法。