/* 未输入数据时,显示当前系统信息
输入的正确格式 java InputDate <xxxx>(年) <xx>(月)"
*/
import java.util.Calendar;
import java.util.GregorianCalendar;
class DateInformation {
private int year;
private int month;
public DateInformation( int year, int month ) {
this.year = year;
this.month = month;
}
private String checkMonth() {
String[] months = { "1 月","2 月","3 月","4 月",
"5 月","6 月","7 月","8 月",
"9 月","10 月","11 月","12 月" };
return months[month];
}
private int checkDays() {
int[] Days = { 31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31 };
return Days[month];
}
public void printDate() {
GregorianCalendar gc = new GregorianCalendar( year, month, 1 );
int week = gc.get( Calendar.DAY_OF_WEEK ) - 1;
System.out.println( "\t\t\t" + year + " 年 " + checkMonth() );
System.out.println();
System.out.println( "\t日\t一\t二\t三\t四\t五\t六" );
for( int i = 0; i < week; i++ ) {
System.out.print( "\t" );
}
for( int i = 1; i <= checkDays(); i++ ) {
if( i <= 9 )
System.out.print( "\t " + i );
else
System.out.print( "\t" + i );
if(( i + week ) % 7 == 0 ) {
System.out.println();
}
}
}
}
class InputDate {
public static void main( String[] args ) {
int year, month;
Calendar cal = Calendar.getInstance();
if( args.length == 2 ) {
year = Integer.parseInt( args[0] );
month = Integer.parseInt( args[1] ) - 1;
}
else{
year = cal.get( Calendar.YEAR );
month = cal.get( Calendar.MONTH );
}
DateInformation di = new DateInformation( year, month );
di.printDate();
}
}
当我输入格式错误时 即 输入 java InputDate 2002 22 时 发生 ArrayIndexOutOfBoundsException 异常
或 输入 java inputDate 2002 x 时 发生 NumberFormatException 异常
我是把整个 printDate() 方法都给列入 try 语句块,看起来很不舒服,感觉自己复杂化了.
请问我该怎样将这些异常添加进去( 最简短的,有效的 )
[此贴子已经被作者于2006-8-2 17:51:05编辑过]