JavaSE实现万年历,javase万年历
分享于 点击 47978 次 点评:209
JavaSE实现万年历,javase万年历
万年历.java
import java.util.Scanner;
public class WanNianLi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入年:");
int year = s.nextInt();
System.out.println("请输入月:");
int month = s.nextInt();
int days = 0;
for (int i = 1900; i < year; i++) {
if (a(i)) {
days += 366;
} else {
days += 365;
}
}
int days1 = getDays(year, month);
days = days + days1;
int weekday = days % 7 + 1;
System.out.println("一\t二\t三\t四\t五\t六\t七\t");
int firstDay = weekday;
for (int i = 1; i < firstDay; i++) {
System.out.print("\t");
}
int currentDays = getDaysInMonth(month, year);
for (int i = 1; i <= currentDays; i++) {
System.out.print(i + "\t");
firstDay++;
if (firstDay == 8) {
firstDay = 1;
System.out.println();
}
}
}
/**
* 计算用户输入月份的天数
*
* @param year用户输入的年
* @param month用户输入的月
* @return返回当月的天数
*/
public static int getDaysInMonth(int month, int year) {
switch (month) {
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;
case 2:
if (a(year)) {
return 29;
} else {
return 28;
}
}
return 0;
}
/**
* 该方法功能是,用户输入年份,判断是否为闰年
*
* @param year用户输入的年份
* @return true是闰年 false是平年
*/
public static boolean a(int year) {
return (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) ? true : false;
}
/**
* 该方法计算从用户输入月份到该年的1月1日有多少天
*
* @param year用户输入的年份,用来判断是否是闰年
* @param month用户输入的月份
* @return 返回一共有多少天
*/
public static int getDays(int year, int month) {
int[] y= new int[]{0,31,28,31,30,31,30,31,31,30,31,30};
int tatol=0;
for (int i = 0; i < month; i++) {
tatol += y[i];
}
return tatol;
}
}
相关文章
- 暂无相关文章
用户点评