admin管理员组

文章数量:1794759

用Java语言判断某一年是否为闰年

用Java语言判断某一年是否为闰年

用Java语言判断某一年是否为闰年

思路分析

1. 用 year 接收用户输入的年份 2. 闰年为: year % 4 = 0 && year % 100 != 0 能被4整除但不能被100整除的年份为普通闰年。 3. year % 400 =0 能被400整除的为世纪闰年。

看代码

import java.util.Scanner; public class HomeWorkThree { public static void main(String[] args) { Scanner myScanner = new Scanner(System.in); System.out.print("请输入需要判断的年份:"); int year = myScanner.nextInt(); if (year > 0) { if (year % 4 == 0 && year % 100 != 0) { System.out.println(year + "为普通闰年."); } else if (year % 400 == 0) { System.out.println(year + "为世纪闰年."); } else { System.out.println(year + "不是闰年!"); } } else { System.out.println("请输入正确的需要判断的年份!"); } } }

本文标签: 闰年语言java