Java怎么编写代码实现判断某个年份是否是闰年?

济南云服务器 2024年6月18日06:29:01Java教程Java怎么编写代码实现判断某个年份是否是闰年?已关闭评论651阅读模式

由于人为历法规定年度天数,与实际地球公转周期存在误差,所以,设立了闰年,下面,济南云服务器小编就来带大家看看,在Java中如何通过编写代码实现判断某一个年份是否是闰年。

关键代码:

public class Main {
    public static void main(String[] args) {
        int year = 2024; // 要判断的年份
        boolean isLeapYear = isLeapYear(year);
        if (isLeapYear) {
            System.out.println(year + "年是闰年");
        } else {
            System.out.println(year + "年不是闰年");
        }
    }

    public static boolean isLeapYear(int year) {
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                return year % 400 == 0;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
}

济南云服务器
  • 本文由 发表于 2024年6月18日06:29:01
  • 转载请务必保留本文链接:http://news.hcsw666.com/1331