用java做一個簡單的萬年曆
阿新 • • 發佈:2018-12-25
一個簡單的萬年曆製作
簡單介紹萬年曆的各功能實現:1.首先鍵盤輸入查詢的年份和月份
Scanner sc = new Scanner(System.in); System.out.println("請輸入年份"); int year = sc.nextInt(); System.out.println("請輸入月份"); int month = sc.nextInt(); //判斷年月是否輸入正確 while(year<1900) { System.out.println("你輸入的年份不正確,請重新輸入年份"); year = sc.nextInt(); } while(month>12||month<=0) { System.out.println("你輸入的月份不正確,請重新輸入月份"); month = sc.nextInt(); }
2.然後對該年份的屬性進行判斷(平年or閏年):
//判斷該年是閏年還是平年
public static boolean YearType(int year)
{
if ((year%4==0 &&year%100!=0)||year%400==0)
{
return true;
}else{
return false;
}
}
3.對輸入年份每個月的天數進行判斷
//判斷該年每個月的天數 public static int day(int month,int year) { if(month==4||month==6||month==9||month==11){ return 30; }else if(month==2){ if(YearType(year)){ return 29; }else{ return 28; } }else{ return 31; } }
4.列印日曆表
public static void WeekTable(int month,int year,int dayall,int monthday){ // System.out.println("日\t"+"一\t"+"二\t"+"三\t"+"四\t"+"五\t"+"六\t"); for(int i=0;i<=day(month,year)+(dayall+monthday)%7;i++) { if(i<=(dayall+monthday)%7){ System.out.print("\t"); }else{ System.out.print((i-(dayall+monthday)%7)+"\t"); } if((i+1)%7==0&&i!=0){ System.out.println(); } } }
5.對以上進行整合,最後附上原始碼:
import java.util.Scanner;
class Calendar
{
public static void main(String[] args)
{
System.out.println("歡迎進入萬曆表查詢系統");
System.out.println("---------------------------------------------------");
boolean button=true;
while(button!=false){
Input();
System.out.println("輸入'true'繼續查詢,'false'退出系統!");
Scanner sc = new Scanner(System.in);
button=sc.nextBoolean();
if(!button){
System.out.println("謝謝你的使用,再見!");
}
}
}
public static void Input(){
int dayall=0,monthday=0;
Scanner sc = new Scanner(System.in);
System.out.println("請輸入年份");
int year = sc.nextInt();
while(year<1900)
{
System.out.println("你輸入的年份不正確,請重新輸入年份");
year = sc.nextInt();
}
System.out.println("請輸入月份");
int month = sc.nextInt();
while(month>12||month<=0)
{
System.out.println("你輸入的月份不正確,請重新輸入月份");
month = sc.nextInt();
}
for (int i=1900+1;i<=year;i++)
{
if(YearType(i)){
dayall+=366;
//System.out.println("閏年");
}else{
dayall+=365;
//System.out.println("平年");
}
}
for (int i=1;i