java如何制作萬年歷-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)java如何制作萬年歷的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

孝昌網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,孝昌網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為孝昌上千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的孝昌做網(wǎng)站的公司定做!

完成萬年歷的制作需要用到數(shù)組、循環(huán)等知識。

編程計算輸入的月份距離1900年1月1日的天數(shù),求出當(dāng)前月之前的總天數(shù)(不包含當(dāng)前輸入月分的天數(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]; 
    } 
   
  } 
   
}

java如何制作萬年歷

感謝各位的閱讀!關(guān)于“java如何制作萬年歷”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

文章題目:java如何制作萬年歷-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://bm7419.com/article42/dssihc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、電子商務(wù)、網(wǎng)站導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)面包屑導(dǎo)航、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)