演算法,java代碼實現列印萬年曆

來源:互聯網
上載者:User

標籤:port   空格   str   log   container   迴圈   萬年曆   ini   多少   

 萬年曆以1900年1月1號星期一為時間原點
星期日 第一天
星期一 第二天
星期二 第三天
星期三 第四天
星期四 第五天
星期五 第六天
星期六 第七天
              1.計算出當前日期距離原點的天數(例:2016/9/18)      2015到1900之間有多少個瑞年和平年-->count1             2016年一月到八月的總天數-->count2      本月的一號      count = count1+count2+1    2.計算出本月的一號是一周的第幾天       k = count%7;(列印一號前面有多少空格)    3.計算出該月有多少天       maxDays    4.迴圈列印       1.for(int i=1;i<=maxDays;i++)       2.k充當指標,到每周的第七天自動換行            
 1 package com.bjsxt.game; 2 import java.util.Scanner; 3 public class Calendar { 4     public static boolean isLeapYear(int year){ 5         if(year%4==0&&year%100!=0 || year%400==0){ 6             return true; 7         } 8             return false; 9     }10     11     //計算當前月份的最大天數12     public static int maxDays(int month,int year){13         14         int maxDays;15         if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)16                 maxDays =  31;17         else if(month==4 || month==6 || month==9 || month==11)18                 maxDays =  30;19         else{20                 if(isLeapYear(year))21                     maxDays = 29;22                 else23                     maxDays = 28;24         } 25         return maxDays;26     }27     28     //計算當前月份一號是一周的第幾天29     public static int count(int month,int year){30         int k;           //表示當前的輸入值是一周的第幾天31         int r = 0, p = 0;32         int count;       //表示距離1970.1.1有多少天33         //計算當前年份之前的瑞年和平年有多少個34         for(int i=1900;i<year;i++){35             if(isLeapYear(i))36                 r++;37             else38                 p++;39         }40         count = 366*r+365*p;41         //判斷當前月份之前的總天數42         for(int i=1;i<month;i++){43                 if(i==2){44                     if(isLeapYear(year))45                         count = count + 29;46                     else47                         count = count + 28;48                 }49                 else if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)50                     count = count + 31;51                 else52                     count =  count + 30;        53         }54         k = (count+1)%7+1;55         return k;56     }57     public static void main(String[] args) {58         Scanner input =  new Scanner(System.in);59         60         System.out.print("請輸入年:");61         int year = input.nextInt();62         63         System.out.print("請輸入月:");64         int month = input.nextInt();65         66         System.out.print("請輸入日:");67         int day = input.nextInt();68         69         int maxDays =  maxDays(month,year); //計算當前輸入的月份最大值是多少天70         int k = count(month, year);         //計算當前月份的1號是第幾天71                 72         System.out.println("日\t一\t二\t三\t四\t五\t六");73         for(int i=0;i<k-1;i++){74             System.out.print(" \t");75         }76         for(int i=1;i<=maxDays;i++){77             System.out.print(i+"\t");78             if(k%7 == 0){79                 System.out.println("\n");80                 k = 0;81             }82             k++;83         }84     }85 }
View Code 

演算法,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.