编写一个具有如下功能的万年历与日历查询系统:显示某一年的全年日历(每天要有对应的星期);显示任意指定某年某月的月历(每天要有对应的星期);显示某年某月某日是星期几;显示某年某月某日是该年的第几天,已知1980年1月1日为星期二。
时间: 2023-05-21 13:04:17 浏览: 108
LCD1602万年历 温度报警显示 闹钟_1682万年历显示温度_闹铃_多功能闹钟_
5星 · 资源好评率100%
好的,以下是万年历与日历查询系统的代码实现。
```
import java.util.Scanner;
public class Calendar {
private static final int[] MONTH_DAYS_COMMON = {31,28,31,30,31,30,31,31,30,31,30,31};
private static final int[] MONTH_DAYS_LEAP = {31,29,31,30,31,30,31,31,30,31,30,31};
private static final String[] WEEKDAYS = {"日", "一", "二", "三", "四", "五", "六"};
// 判断是否为闰年
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
// 计算某年的第一天是星期几
private static int getFirstDayOfWeek(int year) {
int totalDays = 0;
for (int i = 1980; i < year; i++) {
if (isLeapYear(i)) {
totalDays += 366;
} else {
totalDays += 365;
}
}
return (totalDays + 1) % 7;
}
// 获取某年某月的天数
private static int getDaysOfMonth(int year, int month) {
if (isLeapYear(year)) {
return MONTH_DAYS_LEAP[month - 1];
} else {
return MONTH_DAYS_COMMON[month - 1];
}
}
// 显示某一年的全年日历
private static void showYearCalendar(int year) {
System.out.println(year + "年\n");
for (int i = 1; i <= 12; i++) {
showMonthCalendar(year, i);
System.out.println();
}
}
// 显示任意指定某年某月的月历
private static void showMonthCalendar(int year, int month) {
int daysOfMonth = getDaysOfMonth(year, month);
StringBuilder title = new StringBuilder();
title.append(year).append("年").append(month).append("月\n");
for (int i = 0; i < WEEKDAYS.length; i++) {
title.append(WEEKDAYS[i]).append("\t");
}
System.out.println(title); // 打印表头
int firstDayOfWeek = getFirstDayOfWeek(year);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 1; i <= firstDayOfWeek; i++) { // 补充上个月的尾部
stringBuilder.append(" \t");
}
for (int i = 1; i <= daysOfMonth; i++) {
stringBuilder.append(i).append("\t");
if ((firstDayOfWeek + i) % 7 == 0) { // 判断是否到本周的末尾
stringBuilder.append("\n");
}
}
System.out.println(stringBuilder);
}
// 显示某年某月某日是星期几
private static void showWeekday(int year, int month, int day) {
int firstDayOfWeek = getFirstDayOfWeek(year);
int totalDays = 0;
for (int i = 1; i < month; i++) {
totalDays += getDaysOfMonth(year, i);
}
totalDays += day - 1; // 注意日期要减一
System.out.println(WEEKDAYS[(firstDayOfWeek + totalDays) % 7]);
}
// 显示某年某月某日是该年的第几天
private static void showDayOfYear(int year, int month, int day) {
int totalDays = 0;
for (int i = 1; i < month; i++) {
totalDays += getDaysOfMonth(year, i);
}
totalDays += day;
System.out.println(totalDays);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择功能:\n1. 显示某一年的全年日历\n2. 显示任意指定某年某月的月历\n3. 显示某年某月某日是星期几\n4. 显示某年某月某日是该年的第几天\n0. 退出");
int choice = scanner.nextInt();
if (choice == 0) {
break;
}
switch (choice) {
case 1:
System.out.println("请输入年份:");
int year = scanner.nextInt();
showYearCalendar(year);
break;
case 2:
System.out.println("请输入年份:");
year = scanner.nextInt();
System.out.println("请输入月份:");
int month = scanner.nextInt();
showMonthCalendar(year, month);
break;
case 3:
System.out.println("请输入年份:");
year = scanner.nextInt();
System.out.println("请输入月份:");
month = scanner.nextInt();
System.out.println("请输入日期:");
int day = scanner.nextInt();
showWeekday(year, month, day);
break;
case 4:
System.out.println("请输入年份:");
year = scanner.nextInt();
System.out.println("请输入月份:");
month = scanner.nextInt();
System.out.println("请输入日期:");
day = scanner.nextInt();
showDayOfYear(year, month, day);
break;
default:
System.out.println("输入错误,请重新输入!");
break;
}
}
}
}
```
关于中文加密,由于不知道你需要的是哪种加密方式,无法给出具体答案。而对于 "lua closure factory 完整代码" 这个问题,以下是一个简单的例子:
```
-- 闭包工厂,用于创建计数器
function createCounter()
local count = 0
-- 返回一个匿名函数作为计数器
return function ()
count = count + 1
return count
end
end
-- 创建两个计数器
counter1 = createCounter()
counter2 = createCounter()
-- 分别使用两个计数器
print(counter1()) --> 1
print(counter1()) --> 2
print(counter2()) --> 1
print(counter2()) --> 2
print(counter1()) --> 3
```
阅读全文