import java.util.Scanner;
//我的萬年曆
public class MyCalendar
{
public static void main(String[] args)
{
int year;//1900<=year<=2200
int month;//1<=month<=12
int icount = 0;
int monthdays=0;//當月天數
int week;//year年month月1號是星期幾
System.out.println("歡迎使用本系統!");
int[] arr = Scan_in();
year = arr[0];
month = arr[1];
if(isRun(year))
{
System.out.println(year+"年是閏年");
}
else
{
System.out.println(year+"年是平年");
}
System.out.println("您要查詢的是:"+year+"年"+month+"月1日,距離1900年1月1日已經過去了"+TotalDays(year, month)+"天\n");
System.out.println("星期天\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t");
week = TotalDays(year,month)%7;
monthdays = MonthDay(year,month);
for(int i=0;i<week;i++)
{
System.out.print("\t");
icount++;
}
for(int i=1;i<=monthdays;i++)
{
System.out.print(i+"\t");
icount++;
if(icount%7==0)
{
System.out.println();
}
}
}
//輸入年份,顯示當前年份是否閏年
public static boolean isRun(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
return true;//返回真,是閏年
}
else
{
return false;//返回假,是平年
}
}
//輸入年份,月份 計算出當月天數
public static int MonthDay(int year,int month)
{
int MonthDays;//當月天數
switch(month)
{
case 4:
case 6:
case 9:
case 11:
MonthDays = 30;
break;
case 2:
if(isRun(year))
{
MonthDays = 29;
}
else
{
MonthDays = 28;
}
break;
default:MonthDays = 31;
}
return MonthDays;
}
//計算從1900-1-1到當月1號共過了多少天
public static int TotalDays(int year,int month)
{
int TotalDays = 1;//計算從1900-1-1到今天總過過了多少天
for(int i=1900;i<year;i++)
{
if(isRun(i))
{
TotalDays += 366;
}
else
{
TotalDays += 365;
}
}
for(int i=1;i<month;i++)
{
TotalDays += MonthDay(year,i);
}
//System.out.println("從1900-1-1至"+year+"-"+month+"-"+"1共過去了"+TotalDays+"天!");
return TotalDays;
}
public static int[] Scan_in()
{
int[] arr = new int[2];//存儲輸入的年份與月份
boolean flag;
do
{
Scanner in = new Scanner(System.in);
System.out.print("請輸入查詢年份:");
arr[0] = in.nextInt();
System.out.print("請輸入查詢月份:");
arr[1] = in.nextInt();
if((1900<=arr[0]&&arr[0]<=2200)&&(1<=arr[1]&&arr[1]<=12))//判斷年份與月份是否合法
{
flag = false;
}
else
{
System.out.println("輸入的年份與月份不符合國情,請重新輸入!");
flag = true;
}
}
while (flag);
return arr;
}
}