学习记录16

判断闰年import java.util.Scanner;

public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个年份: ");
int year = scanner.nextInt();

    if (isLeapYear(year)) {
        System.out.println(year + " 年是闰年。");
    } else {
        System.out.println(year + " 年不是闰年。");
    }
    scanner.close();
}

public static boolean isLeapYear(int year) {
    // 普通年份能被 4 整除但不能被 100 整除
    boolean condition1 = (year % 4 == 0) && (year % 100 != 0);
    // 世纪年份能被 400 整除
    boolean condition2 = year % 400 == 0;

    return condition1 || condition2;
}

}

posted @ 2025-02-14 23:02  吉尼泰梅  阅读(7)  评论(0)    收藏  举报