package ForCircle;
import java.util.Scanner;
public class Day06 {
// 万年历
public static void timu10() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个年份:");
int year = scanner.nextInt();
System.out.println("请输入一个月份:");
int month = scanner.nextInt();
int week = week(year, month);
for (int i = 0; i < 7; i++) {
System.out.print(weekWord(i) + "\t");
}
System.out.println();
// 输出该月有多少天,从一开始输出
for (int i = 0; i <= monthDay(year, month); i++) {
if (i == 0) {
if(week%7!=0){
for (int j = 0; j < week; j++) {
System.out.print("\t");
}
}
}else{
System.out.print(i + "\t");
if ((week + i) % 7 == 0) {
System.out.println();
}
}
}
}
// 用总天数来计算输入月的第一天的星期数。
public static int week(int year, int month) {
// 计算出多少年到多少年的总天数
int day1 = yearDays(year, year);
// 计算出现在这年已经过了多少天
int day3 = passedDays(year, month);
return (day1+day3)%7;
}
public static String weekWord(int a) {
String string = "";
switch (a) {
case 0:
string = "日";
break;
case 1:
string = "一";
break;
case 2:
string = "二";
break;
case 3:
string = "三";
break;
case 4:
string = "四";
break;
case 5:
string = "五";
break;
case 6:
string = "六";
break;
}
return string;
}
// 返回当月的天数
public static int monthDay(int year, int month) {
if (month == 2) {
return isRun(year) ? 29 : 28;
} else {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
return 31;
} else {
return 30;
}
}
}
// 判断是否为闰年
public static boolean isRun(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}
// 计算出多少年到多少年的总天数
public static int yearDays(int beginYear, int endYear) {
int days = 0;
for (int i = beginYear; i < endYear; i++) {
days += isRun(beginYear) ? 366 : 365;
}
return days;
}
// 计算出这一年已经过了多少天
public static int passedDays(int year, int month) {
int days = 0;
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days += 31;
break;
case 2:
days += isRun(year) ? 29 : 28;
break;
default:
days += 30;
break;
}
}
System.out.println("这" + year + "年已经过了" + (days) + "天");
return days;
}
public static void main(String[] args) {
timu10();
}
}
实现简单的万年历
最新推荐文章于 2024-06-14 23:39:45 发布