出生于1970年,一生有多少个闰年?Java程序计算

本文使用Java程序计算出生于1970年的人,在70年寿命中能遇到多少个闰年。

以下是Java程序,可以计算出一个人出生于1970年,这一生中有多少个闰年:

public class LeapYearCalculator {
    public static void main(String[] args) {
        int birthYear = 1970;
        int lifespan = 70;
        int leapYears = 0;
        
        for (int year = birthYear; year < birthYear + lifespan; year++) {
            if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
                leapYears++;
            }
        }
        
        System.out.println('This person has ' + leapYears + ' leap years in their life.');
    }
}

输出:

This person has 17 leap years in their life.

因此,一个人出生于1970年,这一生中有17个闰年。

出生于1970年,一生有多少个闰年?Java程序计算

原文地址: https://www.cveoy.top/t/topic/j7Y6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录