题目
已知小明生日 1995.7.25,计算出小明从出生到 2020.4.22 一共过了多少天
(需要考虑到闰年, 禁止使用语言本身提供的日期函数)
分析
2020.4.22 - 1995.7.25
注意点:判断平闰年,
判断月份的天数(其中2月份与平润年有关)
1.计算 [ 1995,2020) 左闭右开,一共15年的总天数,即从 1995.01.01 - 2019.12.31 的总天数.
2020 - 2000 = 20 年
2020 - 1995 = 5 年
合计25 年
2.计算起始年的已经过了多少天,即 1995.01.01 - 1995.07.25 的天数
天数1 :7.25 = 31+28+31+30+31+30+31 + 25
3.计算终止年还没过的天数,即 2020.01.01 - 2020.04.22 的天数
天数2 :4.22 = 31+29+31+30 + 22
4.结果 = 25年时间 - 7个月25天 + 4个月22天
代码
import java.util.Scanner;
public class Homework0422 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //创建对象
/*起始年份*/
System.out.print("请输入起始年份:");
int year1 = sc.nextInt(); //起始年份
System.out.print("请输入起始月份:");
int month1 = input.judge_month(sc); //起始月份
System.out.print("请输入起始日期:");
int day1 = input.judge_day(year1, month1, sc); //起始日期
/*最终年份*/
System.out.print("请输入最终年份:");
int year2 = sc.nextInt(); //最终年份
System.out.print("请输入最终月份:");
int month2 = input.judge_month(sc); //最终月份
System.out.print("请输入最终日期:");
int day2 = input.judge_day(year2, month2, sc); //最终日期
/*计算*/
int sum_year = year(year1, year2); //[1995,2020)年总天数,共25年(1995,1996,1997...2019)
int sum_month1 = month(year1, month1); //1995年7个月天数(1,2,3...6)
int sum_month2 = month(year2, month2); //2020年4个月天数(1,2,3)
/*结果*/
int result = sum_year - sum_month1 - day1 + sum_month2 + day2; //15年时间 - 7个月25天 + 4个月22天
System.out.println( "从" + year1 + "/" + month1 + "/" + day1 +"到" + year2 + "/" + month2 + "/" + day2 +"一共过了" + result + "天");
}
/*计算,相差N年有多少天,左闭右开*/
//如,1995到2000年,统计年份为:1995,1996,1997,1998,1999共5年
public static int year(int year1, int year2) { //year1 起始年份,year2 最终年份
int sum = 0; //声明 sum,用来累加天数
for(int i = year1; i < year2; i++) { //左闭右开,统计年数为 year2 - year1
if ( (i % 100 == 0 & i % 400 == 0) || (i % 100 != 0 & i % 4 == 0)) { //判断闰年
sum += 366; //润年,加366
} else {
sum += 365; //平年,加365
}
}
return sum; //年份对应天数的累加和
}
/*计算,几个月有多少天,左闭右开*/
//如,7月,统计月份为:1,2,3,4,5,6,共6个月
public static int month(int year, int month) {
int sum = 0; //声明 sum,用来累加天数
for(int i = 1; i < month; i++) { //左闭右开,统计月数为 month - 1
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) { //天数为31的月份
sum += 31;
} else if (i == 4 || i == 6 || i == 9 || i == 11) { //天数为30的月份
sum += 30;
} else if (i == 2) {
if ((year % 100 == 0 & year % 400 == 0) || (year % 100 != 0 & year % 4 == 0)) { //2月份需判断闰年
sum += 29; //闰年,加29天
} else {
sum += 28; //平年,加28天
}
}
}
return sum; //月份对应天数的累加和
}
}
/*判断用户输入*/
class input{
public static int judge_month(Scanner sc) { //判断用户输入的月份是否合法
while (true) { //循环
boolean b = sc.hasNextInt(); //声明布尔类型,用户输入的为整型时,b为真
if (b) { //判断,用户输入的为整型时,执行下述语句
int temp = sc.nextInt(); //定义临时变量,接收用户输入的整型
if(!(1 <= temp & temp <= 12)){ //判断,用户输入的月份不在 1~12 时,执行下述语句
System.out.println("输入错误,月份需在1~12月内,请重新输入!");
continue; //跳出本次循环,执行下次循环
} else { //判断,用户输入的月份在 1~12 时,执行下述语句
int month = temp; //定义月份,接收用户输入的合法月份
return month; //返回 月份,并结束循环
}
} else { //判断,用户输入的为非数字时,执行下述语句
System.out.println("输入错误,请重新输入!");
sc.next(); //接收输入时的回车键,避免进入死循环
continue; //跳出本次循环,执行下次循环
}
}
}
public static int judge_day(int year, int month, Scanner sc) { //导入用户输入的年份,月份,接收用户输入的日期
boolean temp1 = (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12); //这些月份有31天
boolean temp2 = (month == 4 || month == 6 || month == 9 || month == 11); //这些月份有30天
boolean temp3 = (month == 2); //2月分平闰年
boolean temp_year = ((year % 100 == 0 & year % 400 == 0) || (year % 100 != 0 & year % 4 == 0)); //判断,年份是否是平闰年,闰年为真
while (true) { //循环
boolean b = sc.hasNextInt(); //声明布尔类型,用户输入的为整型时,b为真
if (b) { //判断,用户输入的为数字时,执行下述语句
int temp = sc.nextInt(); //定义临时变量,接收用户输入的整型
int day = 0; //声明并初始化,用以存放用户输入的合法天数
boolean temp4 = ( 1 <= temp && temp <= 31); //日期在1~31为真
boolean temp5 = ( 1 <= temp && temp <= 30); //日期在1~30为真
boolean temp6 = temp_year && ( 1 <= temp && temp <= 29); //闰年,日期在1~29为真
boolean temp7 = (!temp_year) && ( 1 <= temp && temp <= 28); //平年,日期在1~28为真
if( (temp1 && temp4) || (temp2 && temp5) ){ //判断,用户输入的月份对应天数为31天 且 输入的日期在1~31之间,或,用户输入的月份对应天数为30天 且 输入的日期在1~30之间
day = temp; //接收用户输入的合法日期
return day; //返回 天数,并结束循环
}else if( temp3 && temp6 ){ //判断,用户输入的月份为2月 且 是闰年 且 输入的日期在1~29之间
day = temp; //接收用户输入的合法日期
return day; //返回 天数,并结束循环
}else if( temp3 && temp7 ){ //判断,用户输入的月份为2月 且 是平年 且 输入的日期在1~28之间
day = temp; //接收用户输入的合法日期
return day; //返回 天数,并结束循环
}else{ //判断,用户输入均不满足上述情况时,执行下述语句
System.out.println("输入错误,"+ month + "月没有" + temp + "号,请重新输入!");
continue; //跳出本次循环,执行下次循环
}
} else { //判断,用户输入的为非数字时,执行下述语句
System.out.println("输入错误,请重新输入!");
sc.next(); //接收输入时的回车键,避免进入死循环
continue; //跳出本次循环,执行下次循环
}
}
}
}
运行结果
请输入起始年份:1995
请输入起始月份:7
请输入起始日期:25
请输入最终年份:2020
请输入最终月份:4
请输入最终日期:22
从1995/7/25到2020/4/22一共过了9038天
补充
计算月份天数的时候,可以使用数组,能减少适量的代码
/** 定义闰年天数数组与非闰年天数数组 */
int[] dayOfMonth1 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] dayOfMonth2 = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] month = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};