这篇文章主要介绍了怎么用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制作万年历内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。