1 建立日歷表主要步驟
1.1 獲取輸入年距1900年1月1日(星期一)的天數,
1.2 判斷輸入月的第一天是星期幾,
1.3 獲取輸入月的總天數,
1.4 生成日歷表
2 代碼
2.1 判斷閏年 (能被4整除且不能被100整除或能被400整除)
static int text_1(int year) //判斷閏年 是回傳值為 1 { int q; //回傳的值作為判斷 if(year%4==0&&year%100!=0||year%400==0) { q=1; System.out.println("true"); return q; } else { q=0; System.out.println("false"); return q; }
2.2 判斷輸入月份(1,3,5,7,8,10,12月份是31天,4,6,9,11月份是20天,2 月份閏年29天平年28天)
static int text_2(int year, int p_month) // 判斷月份的天數 { int y = text_1(year); // 定義 y 為 判斷閏年的回傳值 int month=0; switch (p_month) { case 2: { int n; n = (y == 1) ? 29 : 28; //判斷 y=1時取29 否則取 28 month=n; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: month=31; break; case 4: case 6: case 9: case 11: month=30; break; default: System.out.println("輸入月份錯誤!"); } System.out.println(month); return month; }
2.3 計算年天數
static int text_3(int year)//計算年距1900年的天數 { int all_year=0; for(int h=1900;h<year;h++) { int u=text_1(h); //呼叫判斷 閏年 h 從1900開始 int i=(u==1)?366:365; //根據回傳值判斷 all_year+=i; //計算天數 } System.out.println(all_year); return all_year; }
2.4 計算月天數
static int text_4(int year,int month) //判斷輸入月前的天數 { int q = 0; int all_month=0; for(int t=1;t<month;t++) { q =text_2(year,t); //呼叫判斷月份 all_month+= q; //計算天數 } System.out.println(all_month); return all_month; }
2.5 計算日歷表排列規律并輸出
static void text_5(int year,int month) { int n = text_2(year, month); //呼叫 判斷月份 int m = text_3(year); //年天數 int k = text_4(year,month); //月天數 int all_day = m + k; //總天數 int month_firstday = all_day + 1; //輸入月份的第一天 int week = month_firstday%7;//判斷第一天是星期幾(周一至周六:1~6 周天:0) int time=week;//判斷第一天前的空間(星期值-1) if(time==0) { time=7; //當week=0時是星期天 } for(int u=0;u<time-1;u++) System.out.print( "\t\t");//輸出空間 for(int y=1;y<=n;y++) { System.out.print(" "+y+"\t\t"); //輸出月的天數 week=week%7; //判斷星期 if(week%7==0) { System.out.println(" "); //星期天時換行 } week++; //星期值+1 } }
3 全部代碼
public class text {
static int text_1(int year) //判斷閏年 是回傳值為 1
{
int q; //回傳的值作為判斷
if(year%4==0&&year%100!=0||year%400==0)
{
q=1;
return q;
}
else
{
q=0;
return q;
}
}
static int text_2(int year, int p_month) // 判斷月份的天數并回傳
{
int y = text_1(year); // 定義 y 為判斷閏年的回傳值
int month=0;
switch (p_month) {
case 2: {
int n;
n = (y == 1) ? 29 : 28; //判斷 y=1時取29 否則取 28
month=n;
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
month=31;
break;
case 4:
case 6:
case 9:
case 11:
month=30;
break;
default:
System.out.println("輸入月份錯誤!");
}
return month;
}
static int text_3(int year)//計算年距1900天數
{
int all_year=0;
for(int h=1900;h<year;h++)
{
int u=text_1(h); //呼叫判斷 閏年 h 從1900開始
int i=(u==1)?366:365; //根據回傳值判斷
all_year+=i; //計算天數
}
return all_year;
}
static int text_4(int year,int month) //判斷輸入月前天數(輸入的年內)
{
int q = 0;
int all_month=0;
for(int t=1;t<month;t++)
{
q =text_2(year,t); //呼叫判斷月份
all_month+= q; //計算天數
}
return all_month;
}
static void text_5(int year,int month) //計算天數排列規律并輸出
{
int n = text_2(year, month); //呼叫 判斷月份
int m = text_3(year); //年天數
int k=text_4(year,month); //月天數
int all_day = m + k; //輸入月前的總天數
int month_firstday = all_day + 1; //輸入月份的第一天
int week=month_firstday%7;//判斷第一天是星期幾
int time=week;//判斷第一天前的空間(星期值-1)
if(time==0)
{
time=7; //當week=0是及星期天
}
for(int u=0;u<time-1;u++)
System.out.print( "\t\t");//輸出空間
for(int y=1;y<=n;y++)
{
System.out.print(" "+y+"\t\t"); //輸出月的天數
week=week%7; //判斷星期
if(week%7==0)
{
System.out.println(" "); //星期天時換行
}
week++; //星期值+1
}
}
public static void main(String[] args) {
Scanner p=new Scanner(System.in);
Scanner q=new Scanner(System.in);
System.out.println("輸入年份");
int w=p.nextInt();
System.out.println("輸入月份");
int u=q.nextInt();
System.out.print("星期一\t"+"星期二\t"+"星期三\t"+"星期四\t"+"星期五\t"+"星期六\t"+"星期天\t");
System.out.println("");
text_5(w,u);
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/217324.html
標籤:其他
上一篇:java建立日歷表
