Java代码 根据输入的年份判断是否为闰年,如果为世纪闰年输出“Century l eap year.”,如果为普通闰年输出“leap year.”,如果不为闰年输出“It i s not leap year.”。并设计测试数据进行判定覆盖测试。
时间: 2024-05-29 17:11:26 浏览: 103
代码如下:
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a year: ");
int year = sc.nextInt();
sc.close();
boolean isLeapYear = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
isLeapYear = true;
}
} else {
isLeapYear = true;
}
}
if (isLeapYear) {
if (year % 100 == 0) {
System.out.println("Century leap year.");
} else {
System.out.println("Leap year.");
}
} else {
System.out.println("It is not leap year.");
}
}
}
测试数据如下:
1. 输入2000,期望输出:Century leap year.
2. 输入2020,期望输出:Leap year.
3. 输入1900,期望输出:It is not leap year.
4. 输入2021,期望输出:It is not leap year.
5. 输入1600,期望输出:Century leap year.
阅读全文