要求:存取年月日的信息
从用户角度出发 存入数据后 由用户规定传入天数,计算机做出相应的计算
这个问题我们要考虑年月日的限制条件以及递推关系
1.年份需要写前给出区间,在进行判断。
2.月份在一到十二月之间。
3.天数的限定在每月的天数可能不同,需要进行设计。(考虑闰年的问题)
下面是源代码:
/*
* Date 存储 年-月-日 信息
* 原则: 一切从用户角度出发
* 功能:
* 1) 初始化
* i. 传入年/月/日
* 2. 不传,今天 回头
* 2) 多少天之后的年/月/日
* 3) 多少天之前的年/月/日
*/
public class Date {
public int year;
public int month;
public int day;
public int[] day_of_month = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
// 构造方法
// 年支持的范围 [1840, 2020]
// 月支持的范围 [1, 12]
// 日支持的范围
public Date(int year, int month, int day) {
// 用户传入参数的合法性校验
if (year < 1840 || year > 2020) {
System.err.println("年的支持范围是 [1840, 2020]");
return;
}
if (month < 1 || month > 12) {
System.err.println("不是地球人的月份");
return;
}
if (day < 1 || day > calcDay