编写程序,应用if语句判断某一年是否为闰年。
程序运行结果如图1所示。
图1 运行结果
l if...else语句的使用。
l 能被4整除并且能被100整除或者能被400整除的年份为闰年。
编写Ex08类,在该类的main方法中编写应用if语句判断2009年是否为闰年的代码,具体代码如下:
package com.wgh;
public class Ex08 {
public static void main(String[] args) {
int year = 2009;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { // 是闰年
System.out.println(year + "是闰年!");
} else { // 不是闰年
System.out.println(year + "不是闰年!");
}
}
}