本文分享了製作萬年曆的方法,如果文中有不正確的地方,歡迎大家批評指正。
(推薦教學:java課程)
程式碼實作:
package com.softeem.test01; import java.time.Month; import java.time.Year; import java.util.Scanner; public class DynamicCalendar { private int y; private int m; public DynamicCalendar(int y, int m) { this.y = y; this.m = m; } public boolean year(int ye) { return ye % 4 == 0 && ye % 100 != 0 || ye % 400 == 0; } public int month(int ye, int mon) { if (mon < 1 || mon > 12) { System.out.println("输入错误"); return 0; } switch (mon) { case 4: case 6: case 9: case 11: return 30; case 2: return year(ye) ? 29 : 28; default: return 31; } } public int method() { int count = 0; for (int i = 1900; i < this.y; i++) { count += year(i) ? 366 : 365; } for (int i = 1; i < m; i++) { count += month(this.y, i); } return count; } public void printCalendar() { int space = method() % 7; int count = month(y, m); System.out.println("==============万年历【" + y + "】年【" + m + "】月==================="); System.out.println("一\t二\t三\t四\t五\t六\t日"); System.out.println("=================================================="); int sum = 0; for (int i = 0; i < space; i++) { System.out.print("\t"); sum++; } for (int i = 1; i < count; i++) { sum++; System.out.print(i + "\t"); if (sum == 7) { System.out.println(); sum = 0; } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入年份:"); int y = sc.nextInt(); System.out.print("请输入月份:"); int m = sc.nextInt(); DynamicCalendar dc = new DynamicCalendar(y, m); dc.printCalendar(); } }
輸出結果:
相關推薦:java入門
#以上是利用java製作萬年曆的詳細內容。更多資訊請關注PHP中文網其他相關文章!
陳述
本文轉載於:csdn。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
Windows 11 KB5054979中的新功能以及如何解決更新問題
4 週前ByDDD
如何修復KB5055523無法在Windows 11中安裝?
3 週前ByDDD
Inzoi:如何申請學校和大學
1 個月前ByDDD
如何修復KB5055518無法在Windows 10中安裝?
3 週前ByDDD
在哪裡可以找到Atomfall中的站點辦公室鑰匙
4 週前ByDDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具