class DayCounter
! q7 j9 p& m5 ]{ ) O( X; y* u O+ J( W: X
public static void main(String[] arguments)
. N" h& C* ]* d, `9 U{
* a& Y2 U2 M+ G! Z( H int yearIn = 2001; //可以改变 年 月的数据
$ P' I' Z/ ?3 x; d9 u/ y int monthIn = 3; 4 Z/ o: p8 G+ i% D- L% |
if (arguments.length > 0) //也可以从命令行上来输入
3 _. v+ w) X* }1 O; g' m1 n# G monthIn = Integer.parseInt(arguments[0]); - r( G& ~, B8 k* D0 j5 Z( Y- [- t
if (arguments.length > 1) 3 T$ q, `( v/ h1 w$ e
yearIn = Integer.parseInt(arguments[1]);
& x g4 q! Q5 n: T a4 D$ } System.out.println(monthIn + "/" + yearIn + " has " + countDays(monthIn, yearIn) + " days.");
3 J4 z/ G" ~& f0 l; ]4 d } $ |* p; ], f1 M9 {$ Y
public static int countDays(int month, int year) , b. h% Y, d% O9 \- @
{ , t, C; T' F: _% o# l, D. q& W
int count = -1;
& O0 c5 l: K. |5 x v( O8 W4 U$ R switch (month) " Q+ u" ?. E8 l! n; S' n
{ 1 F$ i. s" A, ^
case 1: //识别大月 , B0 Q' B O6 ^! i2 b6 h
case 3: 4 L: r* {5 W0 S! ^5 a
case 5:
4 R" n: ?' K, U1 v6 Z: a& f case 7: - ?9 e1 d e3 c! J, m, R
case 8:
& M2 {( M; j1 o T0 C case 10:
: m& H. A$ T* o4 F/ A& i' k case 12: - e* \ d, v8 q
count = 31; 8 H8 J+ b \0 ~
break; ; ~3 G- N9 }- A
case 4: //识别小月 ' X; C- ^4 u/ o) j
case 6:
: s, R! G) z" y/ b( k$ h! {; x' J case 9: " \, U+ W T* G8 m$ N7 h
case 11: ) K( E! I6 a; X+ l
count = 30; ; _; j& I6 _+ O+ n7 d
break; 8 b/ N. k$ ~% e( `+ i& O2 Z
case 2: //识别平月 : U+ k, a- l2 c7 p( Z
if (year % 4 == 0)
& t5 X' ?' N7 q8 u& B8 {- y count = 29;
2 H+ l# S* U7 u4 y4 J# b else & W6 T- K% L* g, G
count = 28; * m# G5 a5 W; S4 U) C( `' v5 ~% {
if ((year % 100 == 0) & (year % 400 != 0))
, z% B& S% {6 X count = 28;
. b* z' M7 {$ y4 Q( G9 \ }
, l- _; H" O! U) |& l return count;
2 U9 C" X/ N: X9 Q }
! u) D/ n, S0 W7 V Z} |