万年历
该设计采用莱单作为应用程序的主要界面,用控制语句来进行程序的执行。该设计的任务是利用一个简单实用的界面完成万年历的显示功能
【任务要求】
1.在输入年份后,出现当年的简易万年历;
2.年历分月份和七列显示
设计要求:
!简单明确的界面完成基本功能功能方法设计
定义 Date 类;
类中包含 getyear , iol eapyear , weekday , monthday , di sp 等成员方法,用于各项判断和显示输出,私有部分为 year ;
3条回答 默认 最新
- 懒羊羊的南瓜屋 2022-06-08 13:51关注
package com.yw.test2; import java.util.Calendar; public class MyDate { private int year; // 农历的年份 private int month; private int day; private int weekday; private int monthday; private boolean leap; private Calendar mCalendar; public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getWeekday() { return mCalendar.get(Calendar.DAY_OF_WEEK)-1; } public int getMonthday() { return mCalendar.get(Calendar.DAY_OF_MONTH); } public boolean isLeap() { Calendar c = Calendar.getInstance(); //设置为那一年的3月1日 c.set(year, 2, 1); //将日向前减去1 c.add(Calendar.DAY_OF_MONTH, -1); //判断是否是29天 return c.get(Calendar.DAY_OF_MONTH) == 29; } public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; mCalendar = Calendar.getInstance(); mCalendar.set(year,month - 1,day); } public MyDate() { mCalendar = Calendar.getInstance(); year = mCalendar.get(Calendar.YEAR); month = mCalendar.get(Calendar.MONTH)+1; day = mCalendar.get(Calendar.DATE); } public void display(){ /*** * 新建对象部分 */ Calendar a = Calendar.getInstance(); /*** * 创建年月日部分 */ a.set(year,month - 1,1); //我在这里1号 /** * 判断用户输入的月份的 1号 是周几 * 是周几前面就有几个空 */ int say1 = a.get(Calendar.DAY_OF_WEEK)-1; System.out.print(year+"年"+month+"月"+day+"日 "); System.out.println(isLeap() ? "闰年" : "非闰年"); System.out.println("今天周" + getWeekday() + " 本月第" + getMonthday() + "天"); System.out.println("日\t一\t二\t三\t四\t五\t六\t"); /*** * 打印一号前面个空格 */ for (int i = 0;i<say1;i++){ System.out.print("\t"); } /** * 打印天数 因为月份开始是从第一天 所以i=1 * i<用户输入月份的总天数+1 i++ * 也可以这样写 * i<=用户输入的总天数 i++ */ for (int i = 1;i<(a.getActualMaximum(Calendar.DAY_OF_MONTH)+1);i++){ /**这里是当星期几变成周日的时候换行 * 这里的逻辑是这样的 * 今天是周六然后打印1号 * 然后say1++变成了7 * 然后打印回车并且清零say1 * 0 1 2 3 4 5 6 * 日 一 二 三 四 五 六 * 在第二行打印2号* */ if(say1 == 7){ System.out.println(); say1 = 0; } System.out.print(i+"\t"); say1++; } } public static void main(String[] args){ MyDate d = new MyDate(); d.display(); } }
OK了,简洁版本
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录