编写一个源程序根据所提供的年份和月份返回该月的天数。
时间: 2024-03-09 09:46:44 浏览: 89
好的,以下是一个 Python 的程序,可以根据输入的年份和月份返回该月的天数:
```python
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
# 判断该年份是否为闰年
is_leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
# 计算该月的天数
if month in [1, 3, 5, 7, 8, 10, 12]:
days = 31
elif month in [4, 6, 9, 11]:
days = 30
else:
if is_leap:
days = 29
else:
days = 28
# 输出结果
print("{0}年{1}月的天数为:{2}".format(year, month, days))
```
你可以将以上代码复制到 Python 环境中运行,然后输入年份和月份,即可得到该月的天数。
相关问题
编写一个java源程序根据所提供的年份和月份返回该月的天数
好的,以下是一个 Java 的程序,可以根据输入的年份和月份返回该月的天数:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入年份:");
int year = input.nextInt();
System.out.print("请输入月份:");
int month = input.nextInt();
// 判断该年份是否为闰年
boolean isLeap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
// 计算该月的天数
int days;
if (month == 2) {
days = isLeap ? 29 : 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
// 输出结果
System.out.println(year + "年" + month + "月的天数为:" + days);
}
}
```
你可以将以上代码复制到 Java 环境中运行,然后输入年份和月份,即可得到该月的天数。
编写一个就爱吧源程序根据所提供的年份和月份返回该月的天数
好的,以下是一个简单的 Python 代码实现:
```python
def get_days_in_month(year, month):
if month == 2:
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return 29
else:
return 28
elif month in [4, 6, 9, 11]:
return 30
else:
return 31
```
该函数接受两个参数:年份和月份,然后返回该月的天数。该函数首先判断该月是否为2月,如果是,则判断该年是否为闰年,如果是,则返回29天,否则返回28天。如果该月不是2月,则判断该月是否为4月、6月、9月或11月,如果是,则返回30天,否则返回31天。