计算从出生到如今共生存了多少天

时间:2022-06-23
本文章向大家介绍计算从出生到如今共生存了多少天,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
import java.util.Scanner;

public class Demo04 {
public static void main(String[] args) {
/*
* 计算从出生到现在过了多少天
* 1、出生那天,还有那年结束的几个月时间
* 2、出生第二年之后,到今年之前的所有整年
* 3、今年从年初到到这个月之前
* 4、这个月过了多少天 这四个部分相加
* */

Scanner sc = new Scanner(System.in);//输入出生年月日
System.out.println("请输入出生的年份");
int startYear = sc.nextInt();
System.out.println("请输入月份");
int startMonth = sc.nextInt();
System.out.println("请输入出生的那天");
int startDay = sc.nextInt();
//输入如今的年月日
System.out.println("请输入今年的年份");
int nowYear = sc.nextInt();
System.out.println("请输入现在是几月");
int nowMonth = sc.nextInt();
System.out.println("请输入今天是几号");
int nowDay = sc.nextInt();
//First
int first = 0;
for(int a = startDay;a <= 12;a++){
switch (a){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
first += 31;
break;
case 4: case 6: case 9: case 11:
first += 30;
break;
default:
if (startYear % 400 == 0|| (startYear % 4 == 0&& startYear % 100 != 0)){
first += 29;
}else{
first += 28;
}
first -= startDay;
}
}

//Second
int second = 0;
for (int b = startYear + 1;b <= nowYear - 1;b++){
if (b % 400 == 0|| (b % 4 == 0 && b % 100 != 0)){
second += 366;
}else{
second += 365;
}
}
//Third
int third = 0;
for (int c = 1; c <= nowMonth - 1;c++){
switch (c){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
third += 31;
break;
case 4: case 6: case 9: case 11:
third += 30;
break;
default:
if (nowDay % 400 == 0|| (nowDay % 4 == 0&& nowDay % 100 != 0)){
third += 29;
}else{
third += 28;
}
}
}

int total = first + second + third + nowDay;
System.out.println(total);



}
}

原文地址:https://www.cnblogs.com/q2001/p/16406464.html