日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

日历实现代码

發布時間:2024/8/1 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 日历实现代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡單日歷的實現

  • 內容介紹:輸入想要顯示的年份以及相應月份,輸入完成后在控制臺中打印出日歷。

  • 代碼實現以及相應流程細節的介紹:

    代碼:

    /*** 簡單日歷的實現:* 輸入所要查找的年分以及月份,* 就可以打印出該年該月份的日歷。* **/ public class Calendar {private int year; private int month;Scanner in = new Scanner(System.in);String[] str = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };public Calendar() {getDate();}public void getDate() { //獲取輸入的年和月。System.out.println("請輸入年份和月份:");year = in.nextInt();month = in.nextInt();}public int ReMonDays() { //返回該月的天數 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 getMonFirst() { /*計算思路:int redays = 1; *1.days用于記錄輸入月份前每一個月份的天數。int days = 0; *2.redays用于記錄輸入月份第一天的天數。for (int i = 0; i < month; i++) { *3.首先redays初始化為1,(一月份第一天switch (i) { *即為本年第一天,加上所求月份前幾個月份天數總和ncase 1: *n+1即為輸入月份在本年中的第n+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;}redays += days; //循環結束后redays即為輸入月份第一天在本年中的第redays天}return ((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + redays) % 7; //計算出輸入月份第一天的星期數并返回。 //共返回七種情況:1,2,3,4,5,6,0對應} //一 二 三 四 五 六 日public void show() {//記錄輸入的月份第一天的星期數int weekday = getMonFirst();//記錄本月份的天數int days = ReMonDays();// 繪制日歷表頭:表頭間隔為四個空格System.out.printf(" %s 月 \n", str[month - 1]);System.out.print("一 二 三 四 五 六 日\n");//根據變量weekday控制“ ”輸出的長度來使每一天對應所在星期//" "共包含由四個空格for (int i = 0; i < (weekday != 0 ? (weekday - 1) : 6); i++) {System.out.print(" "); }//打印各天for (int i = 1; i <= days; i++) {if (i < 10)System.out.printf("0%d ", i);elseSystem.out.printf("%d ", i);if ((weekday + i - 1) % 7 == 0) { //到達星期天后換行System.out.println();}}}public static void main(String[] args) {Calendar c = new Calendar();c.show();}}
  • 顯示樣例:

    總結

    以上是生活随笔為你收集整理的日历实现代码的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。