
用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("请输入正确的需要判断的年份!"); } } }👁️ 阅读量:0
© 版权声明:本文《用Java语言判断某一年是否为闰年》内容均为本站精心整理或网友自愿分享,如需转载请注明原文出处:https://www.zastudy.cn/wen/1686840026a350159.html。