简单日历的代码实现
簡單日歷的實現思路
目錄:
1.相關功能描述
在控制臺上按提示輸入要查詢的年分,
輸入完成后,按下回車鍵即可在控制臺顯示出本年的日歷表。
2.分部對代碼進行分析
首先:總體思路 獲取輸入年分—>判斷并計算出本年每個月的天數—>計算出每個月第一天在本年的第幾天—>顯示日歷
思路的分步以及代碼介紹
(1).獲取輸入
? (2).計算輸入年分每個月份的天數
? 用公式 year % 4 == 0 && year % 100 != 0 || year % 400 == 0判斷輸入年分是平年還是閏年:閏年二月有29天,平年有28天。
public int reDays(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 (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {days = 29;} else {days = 28;}break;}return days;}? (3).計算輸入年分的第一天在本年中屬于第幾天,并由此計算出第一天的星期數
本段代碼中int 型變量用于記錄每個月份的第一天為輸入年份中的第幾天。
變量allDays初始化為1則是因為for循環語句塊在執行過程中未能計算出一月份為本年中第一天
則為后續計算方便規定allDays初始值為1;
某月分前所有天數總和+1的值即為該月份的第一天在輸入年分的天數
根據公式((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + alldays) % 7計算出某一月份第一天的星期數
(4).完成日歷的輸出工作:
完成日歷表頭的輸出
查閱資料知悉日歷表的樣式:三行四列。
用字符串數組存儲每個月份的中文字符總共十二個。
String[] str = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };根據日歷表的樣式:輸出表頭,并根據需要調整日歷表表頭的位置,使其更為美觀。
? 跟據日歷樣式,每個月份天數的高度最多占六行,為保證所有日期正常輸出,則選取最高行數:六行。
? 每行輸出四個月份的日歷。
? 隨后根據 需要的定義成員變量 count 初始值為0
```java int[] num = { 0, 0, 0, 0 }; //用于輔助日期的輸出System.out.println("\t\t\t\t\t\t\t\t2020年\n");for (int i = 0; i < str.length; i++) {if (i < 10) {System.out.printf("\t\t%s 月\t\t", str[i]);} else {System.out.printf("\t\t%s月\t\t", str[i]);}if ((i + 1) % 4 == 0) { // 判斷是否到達四月份的整數倍System.out.println();for (int j = 1; j <= 4; j++) {System.out.print("\t一 二 三 四 五 六 日");}System.out.println();for (int j = 1; j <= 6; j++) { //每個月份天數的高度最多占六行for (int s = 1 + (count * 4); s <= (4 + count * 4); s++) {//每行四個月份System.out.print("\t"); if (j == 1) { //每一個月份第一行的日期根據距離制表符末尾的空格數確定所處星期位置for (int k = 1; k <= (getMonthFirst(s) != 0 ? (getMonthFirst(s) - 1) : 6); k++) { System.out.print(" ");}}for (int m = 1+ num[(s - 1) % 4]; m <= 42/* reDays(s) */; m++) {if (m <= reDays(s)) { //使每個月份的日期輸出,滿六行,不足的部分用空格代替if (m < 10)System.out.printf("0%d ", m);elseSystem.out.printf("%d ", m);} else {System.out.print(" ");}num[(s - 1) % 4]++;if ((getMonthFirst(s) + m - 1) % 7 == 0) { //每個月份輸出一行結束后換行break;}}}System.out.println();}count += 1; // if ((i + 1) % 4 == 0) count自增} //1循環,for (int s = 1 + (count * 4); s //<= (4 + count * 4); s++)進入下一行的月份進行輸出for (int j = 0; j < num.length; j++) { //輸出完一行月份的日歷后,數組的每個元素num[j] = 0; //便于下一行月份日歷的輸出}} }```3.完整代碼以及樣例輸出的呈現
完整代碼
import java.util.Scanner;public class Calender2 {Scanner in = new Scanner(System.in);String[] str = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };private int year;private int count;public Calender2() {getDate();}public void getDate() {System.out.println("請你輸入要查詢年份的日歷表:");this.year = in.nextInt();}public int reDays(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 (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {days = 29;} else {days = 28;}break;}return days;}public int getMonthFirst(int month) {int alldays = 1;for (int i = 0; i < month; i++) {alldays += reDays(i);}return ((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + alldays) % 7;}public void show() {int[] num = { 0, 0, 0, 0 };System.out.println("\t\t\t\t\t\t\t\t2020年\n");for (int i = 0; i < str.length; i++) {if (i < 10) {System.out.printf("\t\t%s 月\t\t", str[i]);} else {System.out.printf("\t\t%s月\t\t", str[i]);}if ((i + 1) % 4 == 0) {System.out.println();for (int j = 1; j <= 4; j++) {System.out.print("\t一 二 三 四 五 六 日");}System.out.println();for (int j = 1; j <= 6; j++) {for (int s = 1 + (count * 4); s <= (4 + count * 4); s++) {System.out.print("\t");if (j == 1) {for (int k = 1; k <= (getMonthFirst(s) != 0 ? (getMonthFirst(s) - 1) : 6); k++) {System.out.print(" ");}}for (int m = 1+ num[(s - 1) % 4]; m <= 42/* reDays(s) */; m++) {if (m <= reDays(s)) {if (m < 10)System.out.printf("0%d ", m);elseSystem.out.printf("%d ", m);} else {System.out.print(" ");}num[(s - 1) % 4]++;if ((getMonthFirst(s) + m - 1) % 7 == 0) {break;}}}System.out.println();}count += 1;}for (int j = 0; j < num.length; j++) {num[j] = 0;}}}public static void main(String[] args) {Calender2 c = new Calender2();c.show();} }顯示輸出樣例
總結
- 上一篇: android心跳包作用,Android
- 下一篇: vue父子组件及非父子组件之间的传值