java用eclipse寫的,先記住幾個快捷鍵,"alt+/"調出整個函數,Ctrl+1 修改錯誤,Ctrl+shift+F 規範代碼,讓代碼看起來更好看 首先位java的結構圖 然後是第一種代碼(第一種完全在主函數中編寫): 第二種代碼將某些功能寫成一塊一塊的函數: 第三種為偷懶的辦法,從主函數開 ...
java用eclipse寫的,先記住幾個快捷鍵,"alt+/"調出整個函數,Ctrl+1 修改錯誤,Ctrl+shift+F 規範代碼,讓代碼看起來更好看
首先位java的結構圖
然後是第一種代碼(第一種完全在主函數中編寫):
1 package com.cal; 2 public class MyCalendar01 { 3 public static void main(String[] args) { 4 int year = 2017; 5 //顯示某一年的日曆 6 System.out.println(year + "年的日曆"); 7 for (int i = 1; i < 13; i++) { 8 int month = i; 9 //顯示某一月的開頭 10 System.out.println(year + "年" + month + "月的日曆"); 11 System.out.println("日\t一\t二\t三\t四\t五\t六"); 12 13 int monthDays = 0; 14 //判斷此月有多少天 15 switch (month) { 16 case 1: 17 case 3: 18 case 5: 19 case 7: 20 case 8: 21 case 10: 22 case 12: 23 monthDays = 31; 24 break; 25 case 4: 26 case 6: 27 case 9: 28 case 11: 29 monthDays = 30; 30 break; 31 case 2: 32 //判斷是否為閏年 33 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 34 monthDays = 29; 35 } else { 36 monthDays = 28; 37 } 38 break; 39 default: 40 break; 41 } 42 //計算每月為星期幾,weekday是某月星期幾 43 int weekDay = 0; 44 //計算1900年到該年之間一共有多少天 45 int days = 0; 46 for (int j = 1900; j < year; j++) { 47 int currentYear = j; 48 if (currentYear % 4 == 0 && currentYear % 100 != 0 49 || currentYear % 400 == 0) { 50 days = days + 366; 51 } else { 52 days = days + 365; 53 } 54 } 55 //算出該年某月之前有多少天 56 for (int j = 1; j < month; j++) { 57 int currentmonth = j; 58 switch (currentmonth) { 59 case 1: 60 case 3: 61 case 5: 62 case 7: 63 case 8: 64 case 10: 65 case 12: 66 days = days + 31; 67 break; 68 case 4: 69 case 6: 70 case 9: 71 case 11: 72 days = days + 30; 73 break; 74 case 2: 75 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 76 days = days + 29; 77 } else { 78 days = days + 28; 79 } 80 break; 81 default: 82 break; 83 } 84 } 85 weekDay = (days % 7 + 1) % 7; 86 //顯示某月主題部分 87 for (int j = 0; j < weekDay; j++) { 88 System.out.print("\t"); 89 } 90 for (int j = 1; j < monthDays + 1; j++) { 91 int day = j; 92 System.out.print(day + "\t"); 93 if ((day + weekDay) % 7 == 0) { 94 System.out.println(); 95 } 96 } 97 System.out.println(); 98 99 } 100 } 101 }
第二種代碼將某些功能寫成一塊一塊的函數:
package com.Calendar; public class MyCalendar { public static void main(String[] args) { int year = 2017; // System.out.println(); //printCalendarofMonth(year, 2); printCalendar(year); } // 顯示每個月的開頭 static void printHeader(int year, int month) { System.out.println(year + "年" + month + "月"); System.out.println("日\t一\t二\t三\t四\t五\t六"); } // 某年某月是否為閏月 static boolean isYear(int year) { boolean result; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { result = true; } else { result = false; } return result; } // 獲得某年的天數 static int getDayofYear(int year) { int days = 0; if (isYear(year) == true) { days = 366; } else { days = 365; } return days; } // 獲得某年某月的天數 static int getDaysofMonth(int year, int month) { int days = 0; switch (month) { case 1: case 3: case 7: case 8: case 10: case 12: case 5: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if (isYear(year) == true) { days = 29; } else { days = 28; } break; default: days = 0; break; } return days; } // 獲得某年某月的1號是星期幾 static int getWeekDayofMonth(int year, int month) { int days = 0; int weekday; // 累計每年天數 for (int i = 1900; i < year; i++) { int currentyear = i; if (isYear(currentyear) == true) { days += 366; } else { days += 365; } } // 累計該年前幾月的天數 for (int j = 1; j < month; j++) { int currentmonth = j; days += getDaysofMonth(year, currentmonth); } weekday = (days % 7 + 1) % 7; return weekday; } // 顯示某月的日曆主題部分 static void printCalendarofMonth(int year, int month) { int days = 0; printHeader(year, month); int weekday = getWeekDayofMonth(year, month); for (int i = 0; i < weekday; i++) { System.out.print("\t"); } days = getDaysofMonth(year, month); for (int i = 0; i < days; i++) { int day = i + 1; System.out.print(day + "\t"); if ((weekday + day) % 7 == 0) { System.out.println(); } } System.out.println(); } //顯示某年日曆 static void printCalendar(int year){ for (int i = 1; i <13; i++) { int month=i; printCalendarofMonth(year, month); } } }
第三種為偷懶的辦法,從主函數開始寫分支函數,然後用ctl+1來建立分支函數,可以更快點:
package com.cal; public class MyCalendar { public static void main(String[] args) { int year = 2017; //System.out.println(getDays(year,2)); //列印某一年的日曆 printYearCalendar(year); } private static void printYearCalendar(int year) { System.out.println(year + "年的日曆"); for (int i = 0; i < 12; i++) { int month = i + 1; System.out.println(year + "年" + month + "月"); System.out.println("日\t一\t二\t三\t四\t五\t六"); printMonthofHeader(year, month); } } private static void printMonthofHeader(int year, int month) { // 某月的開始為星期幾 int weekday = getFirstDayofMonth(year, month); for (int i = 0; i < weekday; i++) { System.out.print("\t"); } //根據年份和月份得到某個月共有幾天 int days = getDays(year, month); for (int i = 0; i < days; i++) { int day = i + 1; System.out.print(day + "\t"); if ((weekday + day) % 7 == 0) { System.out.println(); } } System.out.println(); } //獲得某年某個月的1號是星期幾 private static int getFirstDayofMonth(int year, int month) { int weekday; int days=0; // 計算從1900年1月1日以來的整年天數 for (int i = 1900; i <year; i++) { int currentyear=i; days+=getDaysofYear(currentyear); } // 計算當前年份當前月份之前整月的天數 for (int i = 1; i < month; i++) { int currentmonth=i; days+=getDays(year, currentmonth); } // 根據總天數計算這一天是星期幾 weekday=(days%7+1)%7; return weekday; } //獲得某年的天數 private static int getDaysofYear(int year) { if(isLeapYear(year)==true){ return 366; }else{ return 365; } } //根據年份和月份得到某個月共有幾天 private static int getDays(int year, int month) { int days = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days=30; break; case 2: if(isLeapYear(year)==true){ days=29; }else{ days=28; } break; default: days = 0; break; } return days; } //判斷某個年份是否為閏年 private static boolean isLeapYear(int year) { boolean result; if(year%4==0&&year%100!=0||year%400==0){ result= true; }else{ result= false; } return result; } }