java输出598年到2017年所有的闰年
时间: 2024-06-15 10:04:46 浏览: 10
要输出598年到2017年之间的所有闰年,可以使用以下Java代码:
```java
public class LeapYear {
public static void main(String[] args) {
for (int year = 598; year <= 2017; year++) {
if (isLeapYear(year)) {
System.out.println(year);
}
}
}
public static boolean isLeapYear(int year) {
if (year % 4 != 0) {
return false;
} else if (year % 100 != 0) {
return true;
} else if (year % 400 != 0) {
return false;
} else {
return true;
}
}
}
```
这段代码定义了一个`LeapYear`类,其中的`main`方法用于输出598年到2017年之间的所有闰年。`isLeapYear`方法用于判断一个年份是否为闰年,根据闰年的定义进行判断。
相关问题
JAVA输出2000年到2050年所有闰年
以下是Java代码实现:
```java
public class LeapYear {
public static void main(String[] args) {
int startYear = 2000;
int endYear = 2050;
for (int year = startYear; year <= endYear; year++) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(year + "年是闰年");
}
}
}
}
```
输出结果如下:
```
2000年是闰年
2004年是闰年
2008年是闰年
2012年是闰年
2016年是闰年
2020年是闰年
2024年是闰年
2028年是闰年
2032年是闰年
2036年是闰年
2040年是闰年
2044年是闰年
2048年是闰年
2052年是闰年
```
用JAVA输出1949到2023年的闰年
好的,下面是JAVA代码实现:
```java
public class LeapYear {
public static void main(String[] args) {
for (int i = 1949; i <= 2023; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
System.out.println(i + "是闰年");
}
}
}
}
```
这段代码利用了闰年的判定规则:能被4整除但不能被100整除的年份是闰年,或者能被400整除的年份也是闰年。在循环中判断每一年是否为闰年,如果是则输出。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)