从键盘输入年份和月份, 输出该月份的日历表【需求】: (1) 从键盘输入年份和月份。(2)输出为:该月份对应的日历表。
时间: 2023-11-28 09:47:45 浏览: 164
为了实现这个需求,我们可以使用Python中的calendar模块。下面是一个实现该功能的完整代码:
```python
import calendar
# 从键盘读取年份和月份
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
# 使用calendar模块生成日历表
cal = calendar.month(year, month)
# 输出日历表
print(cal)
```
在运行该程序时,会要求你输入年份和月份。输入完之后,程序会生成并输出对应的日历表。例如,如果你输入2021年1月,那么程序会输出以下内容:
```
January 2021
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
```
这就是2021年1月份的日历表。
相关问题
使用Java完成从键盘输入年份和月份, 输出该月份的日历表【需求】: (1) 从键盘输入年份和月份。(2)输出为:该月份对应的日历表。
以下是使用Java实现从键盘输入年份和月份,输出该月份的日历表的代码:
```java
import java.util.Scanner;
public class Calendar {
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();
// 获取该月份的天数
int days = getDaysOfMonth(year, month);
// 获取该月份第一天是星期几
int firstDayOfWeek = getFirstDayOfWeek(year, month);
// 输出日历表头
System.out.println("日\t一\t二\t三\t四\t五\t六");
// 输出该月份的日历表
int count = 1;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
if ((i == 0 && j < firstDayOfWeek) || count > days) {
System.out.print("\t");
} else {
System.out.print(count + "\t");
count++;
}
}
System.out.println();
}
}
// 获取该月份的天数
public static int getDaysOfMonth(int year, int month) {
int days;
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
return days;
}
// 获取该月份第一天是星期几
public static int getFirstDayOfWeek(int year, int month) {
int dayOfWeek;
int century = year / 100;
year = year % 100;
if (month == 1 || month == 2) {
month += 12;
year--;
}
dayOfWeek = (1 + 26 * (month + 1) / 10 + year + year / 4 + century / 4 + 5 * century) % 7;
return dayOfWeek;
}
}
```
运行程序,输入年份和月份,即可输出该月份的日历表。例如,输入2021和7,即可输出2021年7月的日历表:
```
请输入年份:2021
请输入月份:7
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
```
java从键盘输入年份和月份, 输出该月份的日历表:
以下是一个基于控制台的 Java 代码示例,用于输入年份和月份,然后输出该月份的日历表:
```java
import java.util.Scanner;
public class PrintCalendar {
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();
// 计算当月第一天是星期几
int firstDayOfWeek = getFirstDayOfWeek(year, month);
// 输出日历表头
System.out.printf("%10s%s\n", "", getMonthName(month) + " " + year);
System.out.println("-------------------------------------------------");
System.out.printf("%s%s%s%s%s%s%s\n", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
// 输出日历表格
for (int i = 0; i < firstDayOfWeek; i++) {
System.out.printf("%4s", "");
}
for (int i = 1; i <= getNumDaysInMonth(year, month); i++) {
System.out.printf("%4d", i);
if ((i + firstDayOfWeek) % 7 == 0) {
System.out.println();
}
}
}
// 获取指定年份、月份的第一天是星期几(0 表示星期日,1 表示星期一,依次类推)
public static int getFirstDayOfWeek(int year, int month) {
int day = 1;
if (month < 3) {
month += 12;
year--;
}
int century = year / 100;
year %= 100;
int weekday = (day + 13 * (month + 1) / 5 + year + year / 4 + century / 4 + 5 * century) % 7;
return (weekday + 6) % 7;
}
// 获取指定月份的天数
public static int getNumDaysInMonth(int year, int month) {
int numDays = 0;
switch (month) {
case 1: numDays = 31; break;
case 2:
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
numDays = 29;
} else {
numDays = 28;
}
break;
case 3: numDays = 31; break;
case 4: numDays = 30; break;
case 5: numDays = 31; break;
case 6: numDays = 30; break;
case 7: numDays = 31; break;
case 8: numDays = 31; break;
case 9: numDays = 30; break;
case 10: numDays = 31; break;
case 11: numDays = 30; break;
case 12: numDays = 31; break;
default: break;
}
return numDays;
}
// 获取指定月份的名称
public static String getMonthName(int month) {
String monthName = "";
switch (month) {
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
default: break;
}
return monthName;
}
}
```
运行代码后,会提示输入年份和月份,然后输出该月份的日历表。例如,输入 2022 和 9,会输出如下的日历表:
```
September 2022
-------------------------------------------------
SunMonTueWedThuFriSat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
```
阅读全文