判断一个年份是否为闰年
时间: 2023-09-18 13:13:26 浏览: 57
判断一个年份是否为闰年的方法是:
1. 如果该年份能被4整除但不能被100整除,则是闰年;
2. 如果该年份能被400整除,则也是闰年;
3. 其他情况都不是闰年。
下面是一个 Python 代码示例:
```python
def is_leap_year(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True
else:
return False
```
其中,`year` 是要判断的年份,函数返回 `True` 表示是闰年,返回 `False` 表示不是闰年。
相关问题
如何判断一个年份是否为闰年?
以下是两种判断一个年份是否为闰年的方法:
1. 闰年的判断方式:能被4整除不能被100整除的是闰年;能被400整除的是闰年。
```python
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
# 示例
print(is_leap_year(2000)) # 输出:True
print(is_leap_year(1900)) # 输出:False
```
2. 将判断放在一个if语句中:
```python
year = int(input("请输入你要判断的年份: "))
if ((year % 4 == 0 and year % 100 != 0) or year % 4 == 0):
print("该年份为闰年!")
else:
print("该年份不是闰年!")
```
用java判断一个年份是否为闰年
判断一个年份是否为闰年的方法如下:
```java
public static boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}
```
其中,闰年的判断条件为:
1. 能被4整除但不能被100整除的年份为闰年。
2. 能被400整除的年份也是闰年。
例如:
```java
System.out.println(isLeapYear(2000)); // true
System.out.println(isLeapYear(2004)); // true
System.out.println(isLeapYear(1900)); // false
System.out.println(isLeapYear(2021)); // false
```