java實驗之列印萬年曆

來源:互聯網
上載者:User

標籤:r++   art   public   system.in   else   break   java   輸出   imp   

列印萬年曆(輸入年份,月份,輸出該月的日曆,已知1900年1月1日是星期一),要求:

(1)編寫一個方法判斷閏年;

(2)編寫一個方法判斷某年某月有多少天;

(3)編寫一個方法計算某年某月前距離1900年1月1日的總天數;(4)編寫一個輸出某年某月日曆的方法;

(5)編寫一個測試方法。

 

package calendar;
import java.util.Scanner;
public class calendar {
public static void main(String args[]){
int year,month,sum,sum1;
System.out.print("請輸入年份:\t");
for(;;){
year=getInput();
if(year>=1900)
break;
else
System.out.println("輸入錯誤,請重新輸入!");

}
System.out.print("請輸入月份:\t");
for(;;){
month=getInput();
if(month>=1&&month<=12)
break;
else
System.out.println("輸入錯誤,請重新輸入!");
}
display(year,month);
}
public static int getInput(){ //輸入資料
Scanner scan=new Scanner(System.in);
int i=0;
i=scan.nextInt();
return i;
}
public static int judge(int i){ //判斷是否閏年
if(i%400==0||(i%100!=0&&i%4==0))
return 0;
else
return 1;
}
public static int monthTotal(int i,int j){ //判斷i年j月有多少天
int leapyear[]={31,29,31,30,31,30,31,31,30,31,30,31};
int noleapyear[]={31,28,31,30,31,30,31,31,30,31,30,31};
int n,m;
n=judge(i);
if(n==0)
m=leapyear[j-1];
else
m=noleapyear[j-1];
return m;
}
public static int yearTotal(int i,int j){ //判斷i年j月前距1900年1月1日有多少天
int n,m=0;
for(int yearNumber=1900;yearNumber<i;yearNumber++){
if(i==1900)
break;
n=judge(yearNumber);
if(n==0)
m+=366;
else
m+=365;
}
for(int monthYear=1;monthYear<j;monthYear++){
if(j==1)
break;
m+=monthTotal(i,monthYear);
}
return m;
}
public static void display(int i,int j){ //輸出i年j月的日曆
int sum,sum0,ss=0;
sum0=yearTotal(i,j);
sum=1+sum0%7;
System.out.println("-----"+i+"年"+j+"月-----");
System.out.println(" 日 一 二 三 四 五 六 ");
for(int z=1;z<=sum+monthTotal(i,j);z++){
if(z<=sum){
System.out.print(" ");
ss+=1;
}
else{
System.out.print(z-sum+" ");
ss+=1;
if(z-sum<10)
System.out.print(" ");
}
if(ss%7==0)
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.