萬年曆(java實現)

來源:互聯網
上載者:User

標籤:

設計思路:

以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實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.