標籤:
設計思路:
以1900年1月1日(該年1月1日為周一)為基準,計算這一天與輸入的日期的那一個月的第一天的距離totalDay,(例如:輸入2015 6 6)則計算 1900 1.1 到 2015年 6.1;
用totalDay取餘7 + 1,就能得到輸入日期那一個月的第一天是周幾,然後判斷該月的天數輸出就行了。
1 ,3 ,5 ,7 ,8 ,10, 12月有31天,2月閏年29天,平年28天;其餘月份30天;
年份是4的倍數但不是100的倍數,或者年份是400的倍數,則該年為閏年;
import java.util.*;public class calendar{public static void main(String[] args){System.out.println("請輸入日期");Scanner in=new Scanner(System.in);int year=in.nextInt();int month=in.nextInt();int days=in.nextInt();int cnt=0;//計算年差,累計的日期(距離1900年1月1日)(1900年1月1日為周一)for(int i=1900;i<year;i++){if(i%4==0&&i%100!=0||i%400==0)cnt+=366;elsecnt+=365;}int cnt2=0;//計算月差累計的日期for(int i=1;i<month;i++){if(i==2){if(year%4==0&&year%100!=0||year%400==0)cnt2+=29;elsecnt2+=28;}else if(i==4||i==6||i==9||i==11)cnt2+=30;else cnt2+=31;}int totalDay=cnt+cnt2+1;//輸入日期的月份的第一天與1900年1月1日的日期差 //System.out.println(totalDay);int monweek=(totalDay%7); //判斷輸入日期的月份的第一天是星期幾//System.out.println(monweek);System.out.println("日\t一\t二\t三\t四\t五\t六");int limit;//該月有幾天if(month==2){if(year%4==0&&year%100!=0||year%400==0)limit=29;elselimit=28;}else if(month==4||month==6||month==9||month==11)limit=30;elselimit=31;int k=1;for(int i=0;i<monweek;i++)System.out.print("\t");while(k!=limit+1){System.out.print(k++ +"\t");monweek++;if(monweek%7==0)System.out.println();}System.out.println();}}
萬年曆(java實現)