Program:打印萬年歷(輸入年份,月份,輸出該月的日歷,已知1900年1月1日是星期一),
要 求:
(1)編寫一個方法判斷閏年;
(2)編寫一個方法判斷某年某月有多少天;
(3)編寫一個方法計算某年某月前距離1900年1月1日的總天數;(4)編寫一個輸出某年某月日歷的方法;
(5)編寫一個測試方法。
Description:該項目由多個類實現,最后類為main方法的調用。代碼如下:
1 /* 2 *Description:定義工具類 3 * 4 * */ 5 6 package tools; 7 8 import java.util.Scanner; 9 10 public class Operate { 11 12 //定義方法,判斷一個年是否是閏年 13 public static boolean ifLeapyear(int year) { 14 15 if( year % 400 == 0 ) { //能被400整除 16 17 return true; 18 }else if( year % 100 != 0 && year % 4 == 0 ) { //不是100倍數,但是能被4整除 19 20 return true; 21 }else { 22 23 return false; 24 } 25 } 26 27 //判斷某年某月有多少天 28 public static int getDays(String yearMonth) { 29 30 String array[] = yearMonth.split("-"); 31 int year = Integer.parseInt( array[0] ); 32 int month = Integer.parseInt( array[1] ); 33 int monthOne[] = {31,29,31,30,31,30,31,31,30,31,30,31}; 34 int monthTwo[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 35 36 if( Operate.ifLeapyear(year) ) { 37 return monthOne[month-1]; 38 } 39 return monthTwo[month-1]; 40 } 41 42 //接收用戶的輸入 43 public static String getInput() { 44 45 Scanner scan = new Scanner(System.in); 46 String yearMonth = ""; 47 System.out.println( "請輸入年月,中間用'-'分開:" ); 48 yearMonth = scan.next(); 49 50 return yearMonth; 51 } 52 53 //驗證用戶輸入數據的合法性 54 public static boolean validate(String yearMonth) { 55 56 if( yearMonth.matches( "\\d{4}\\W\\d{1,2}" ) ) { //進行正則匹配 57 58 String str[] = yearMonth.split("-"); 59 60 if( Integer.parseInt( str[0] ) < 1900 ) { //年份小於1990 61 return true; 62 } 63 64 //月份不在1-12月之間 65 if( Integer.parseInt(str[1]) < 1 || Integer.parseInt( str[1] ) > 12 ) { 66 67 return true; 68 } 69 70 return false; 71 72 }else { 73 return true; 74 } 75 } 76 77 //計算某年某月距離1990年有多少天 78 public static int getAllDays(int year,int month,int[] monthOne,int[] monthTwo) { 79 int count = 0; 80 for( int i = 1900;i < year; i++ ) { 81 82 if( Operate.ifLeapyear(i) ) { 83 count += 366; 84 }else { 85 86 count += 365; 87 } 88 } 89 90 for( int i = 0; i < month - 1; i++ ) { 91 92 if( Operate.ifLeapyear(year) ) { 93 94 count += monthOne[i]; 95 }else { 96 97 count += monthTwo[i]; 98 } 99 } 100 return count; 101 } 102 103 104 //打印日歷 105 public static void printDate(String yearMonth) { 106 String array[] = yearMonth.split("-"); 107 int year = Integer.parseInt( array[0] ); 108 int month = Integer.parseInt( array[1] ); 109 int monthOne[] = {31,29,31,30,31,30,31,31,30,31,30,31}; //閏年每個月的天數 110 int monthTwo[] = {31,28,31,30,31,30,31,31,30,31,30,31}; //平年每個月的天數 111 int count = 0; 112 113 count = Operate.getAllDays(year, month, monthOne, monthTwo); //得到天數 114 115 int remainDays = count % 7; //除以7。看看剩余的天數 116 int monthDays = Operate.getDays(yearMonth); //得到目標月份的天數 117 String[] days = new String[remainDays + monthDays + 2]; //開辟一個String數組,存放打印的內容 118 119 int k = 1; 120 for(int i = 0; i < days.length; i++) { //為數組賦值 121 122 if( i < remainDays + 1 ) { 123 days[i] = " "; 124 }else { 125 days[i] = k + ""; 126 k++; 127 } 128 } 129 130 //打印出rili 131 System.out.println( "---------------------" + year + "年" + month + "月" + "---------------------" ); 132 System.out.println( "日\t一\t二\t三\t四\t五\t六" ); 133 for( int i = 0; i < days.length; i++ ) { 134 135 System.out.print(days[i] + "\t"); 136 if( (i + 1) % 7 == 0 ) { //每七天換行 137 System.out.println(); 138 } 139 } 140 } 141 142 }
1 /* 2 * Description:打印某年某月的日歷 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-09-24 7 * 8 * */ 9 10 package main; 11 import tools.Operate; 12 13 14 public class DemoTwo2 { 15 16 public static void main(String args[]) { 17 18 String input = ""; //接收用戶輸入的值 19 boolean flag = true; 20 while( flag ) { //判斷用戶輸入的日期是否合法 21 input = Operate.getInput(); 22 if( Operate.validate(input) ) { //調用方法檢測 23 24 System.out.println( "輸入日期格式錯誤,請重新輸入:" ); 25 }else { 26 27 flag = false; 28 } 29 } 30 31 Operate.printDate(input); //打印rili 32 } 33 }
