Java练习:从控制台输入一个年份,统计输入的年份到现在一共多少天?

从控制台输入一个年份,统计输入的年份到现在一共多少天?
注意事项:
1、平年365天,闰年十366天。2月份平年28天,2月份闰年29年。
2、今年2022年3月14日
3、闰年判断:能够被四百整数;能被4整数但不能被100整数
public static void main(String[] args) { int sum = 0; System.out.println("请输入一个年份?"); Scanner scanner = new Scanner(System.in); int year = scanner.nextInt(); while (year <= 2022) { // 闰年 if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) { sum += 366; // 2022年 } else if (year == 2022) { sum = sum + 31 + 28 + 14; // 平年 } else { sum += 365; } year++; } System.out.println("距今的天数" + sum); }
输入:2021,输出:距今的天数438
请输入一个年份? 2021 距今的天数438
本文作者:刘广法,转载注明出处。