java leap year简单代码
时间: 2023-05-17 09:05:11 浏览: 126
以下是 Java 中判断闰年的简单代码:
```java
public class LeapYear {
public static void main(String[] args) {
int year = 2020;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
```
这段代码可以判断给定的年份是否为闰年,如果是闰年则输出 "is a leap year.",否则输出 "is not a leap year."。
相关问题
Java代码 根据输入的年份判断是否为闰年,如果为世纪闰年输出“Century l eap year.”,如果为普通闰年输出“leap year.”,如果不为闰年输出“It i s not leap year.”。并设计测试数据进行判定覆盖测试。
代码如下:
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.
写一段Java代码 根据输入的年份判断是否为闰年,如果为世纪闰年输出“Century l eap year.”,如果为普通闰年输出“闰年”。,如果不为闰年输出“It i s not leap year.”。并设计测试数据进行判定覆盖测试。
```
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if (year % 400 == 0) {
System.out.println("Century leap year.");
} else if (year % 4 == 0 && year % 100 != 0) {
System.out.println("Leap year.");
} else {
System.out.println("It is not leap year.");
}
}
}
```
测试数据:
输入:2000,预期输出:Century leap year.
输入:2004,预期输出:Leap year.
输入:1900,预期输出:It is not leap year.
输入:2021,预期输出:It is not leap year.
阅读全文