package com.qianfeng.test;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;public class Test05 {
public static void main(String[] args) {
// 4万年历看图要求进行分析和书写
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要查询的年份:");
int year = sc.nextInt();
System.err.println("请输入需要查询的月份:");
int month = sc.nextInt();
//System.out.println(isLeapYear(year));
int days=getCurrentDays(month, year);
//System.out.println(days);
//获得1900年距离当前年份上一年的总天数
int predays = sumDays(year);
//System.out.println(predays);
//获得当前年份所经过的天数
int curdays = currentDays(month, year);
//System.out.println(curdays);
int weekdays = (curdays+predays+1)%7;
getFirstWeekDays(weekdays,month,year);
}
//求得给定月份第一天的星期数
public static void getFirstWeekDays(int weekdays,int month,int year) {
int cnt = 0;System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
for (int i = 0; i < weekdays; i++) {
System.out.print("\t");
cnt++;
}
for (int i = 1; i <=getCurrentDays(month, year); i++) {
System.out.print(i+"\t");
cnt++;
if(cnt%7==0) {
System.out.println();
}
}
}
//获得当前年份所经过的天数
public static int currentDays(int month ,int year) {
int curdays = 0;
for (int i = 1; i <month; i++) {
curdays +=getCurrentDays(i, year);
}
return curdays;
}
//获得1900年距离当前年份上一年的总天数
public static int sumDays(int year) {
int sdays = 0;
for (int i = 1900; i <year; i++) {
sdays += isLeapYear(i)? 366:365;
//System.out.println(sdays);
}
return sdays;
}
//根据用户输入的月 来判断 月 的天数
public static int getCurrentDays(int month,int year) {
int days = 0;
switch (month) {
case 2:
if(isLeapYear(year))
return 29;
else {
return 28;
}
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
}
return 0;
}
//根据用户输入的年,先判断是否是闰年
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}
Java——万年历的实现
猜你喜欢
转载自blog.csdn.net/fang1025780636/article/details/81258556
今日推荐
周排行