[java]代码库
import java.util.Scanner;
public class code1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入你想查询的年份(至少为1900年):");
int year = in.nextInt();
System.out.println("请输入你想查询的月份:");
int month = in.nextInt();
int flag = 0;
while (flag == 0) {
int sum = 0;
int num = 0;
for (int date = 1900; date < year; date++) {
if (date % 4 == 0 && date % 100 != 0 || date % 400 == 0) {
sum = sum + 366;
} else {
sum = sum + 365;
}
}
for (int date1 = 1; date1 < month; date1++) {
if (date1 == 1 || date1 == 3 || date1 == 5 || date1 == 7 || date1 == 8 || date1 == 10 || date1 == 12) {
sum = sum + 31;
} else if (date1 == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
sum = sum + 29;
} else {
sum = sum + 28;
}
} else {
sum = sum + 30;
}
}
sum = sum + 1;
System.out.println("**************************");
System.out.println("\t下面是"+year+"年"+month+"月"+"的日历");
System.out.println("**************************");
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int i = 0; i < (sum % 7); i++) {
System.out.print(" \t");
}
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 | month == 12) {
num = 31;
} else if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
num = 29;
} else {
num = 28;
}
} else {
num = 30;
}
for (int i = 1; i <= num; i++, sum++) {
System.out.print(i);
if (sum % 7 == 6) {
System.out.println();
} else {
System.out.print("\t");
}
}
System.out.println();
System.out.println("请选择1、2、3");
System.out.println("1、观看上一个月的日历");
System.out.println("2、观看下一个月的日历");
System.out.println("3、结束");
int select = in.nextInt();
if (select == 1) {
month = month - 1;
if (month == 0) {
year = year - 1;
month = 12;
}
flag = 0;
} else if (select == 2) {
month = month + 1;
if (month == 13) {
year = year + 1;
month = 1;
}
flag = 0;
} else {
flag = 1;
}
}
}
}