非常實用的java萬年歷制作方法
更新時間:2018年02月22日 09:06:51 作者:lx__angel
這篇文章主要為大家詳細介紹了非常實用的java萬年歷制作方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
完成萬年歷的制作需要用到數(shù)組、循環(huán)等知識。
編程計算輸入的月份距離1900年1月1日的天數(shù),求出當前月之前的總天數(shù)(不包含當前輸入月分的天數(shù),)
編程計算輸入月份的第一天是星期幾,(公式:星期幾=1+天數(shù)差%7)。
import java.util.Scanner; public class Calendar{ public static void main(String[] args){ int year; int month; int totaldays=0; Scanner sc=new Scanner(System.in); System.out.println("請輸入年"); year=sc.nextInt(); System.out.println("請輸入月"); month=sc.nextInt(); //計算年的總天數(shù) for(int i=1900;i<year;i++){ if((i%400==0)||(i%4==0&&i%100!=0)){ totaldays+=366; }else{ totaldays+=365; } } //距離1900年1月1好的總天數(shù) totaldays+=monthdays(month,year); //System.out.println(totaldays); System.out.println("-------------"+year+"年"+month+"月日歷為---------------"); //開頭 System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t"); //該月第一天是星期幾,星期幾前面就空幾格 int x=1+totaldays%7; if(x==7){ x=0; } for(int i=0;i<x;i++){ System.out.print(" \t"); } int days=monthday(month,year); int i=1; while(i<=days){ System.out.print(i+" \t"); if((i+x)%7==0){ System.out.println(); } i++; } } //月份總天數(shù) public static int monthdays(int month,int year){ int totaldays=0; for(int i=1;i<month;i++){ totaldays+=monthday(i,year); } return totaldays; } //某月天數(shù) public static int monthday(int month,int year){ if((year%400==0)||(year%4==0&&year%100!=0)){ int[] arr={0,31,29,31,30,31,30,31,31,30,31,30,31}; return arr[month]; }else{ int[] arr={0,31,28,31,30,31,30,31,31,30,31,30,31}; return arr[month]; } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring循環(huán)依賴正確性及Bean注入的順序關系詳解
這篇文章主要給大家介紹了關于Spring循環(huán)依賴的正確性,以及Bean注入的順序關系的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01深入學習JavaWeb中監(jiān)聽器(Listener)的使用方法
這篇文章主要為大家詳細介紹了深入學習JavaWeb中監(jiān)聽器(Listener)的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09詳解springboot+atomikos+druid?數(shù)據(jù)庫連接失效分析
本文主要介紹了springboot+atomikos+druid?數(shù)據(jù)庫連接失效分析,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02MyBatis中insert操作返回主鍵的實現(xiàn)方法
在使用MyBatis做持久層時,insert語句默認是不返回記錄的主鍵值,而是返回插入的記錄條數(shù)。這篇文章主要介紹了MyBatis中insert操作返回主鍵的方法,需要的朋友可以參考下2016-09-09Java ArrayDeque實現(xiàn)Stack的功能
這篇文章主要介紹了Java ArrayDeque實現(xiàn)Stack功能的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03