帮忙看看这个日历程序,报数组越界异常。
发布网友
发布时间:2022-05-27 18:15
我来回答
共1个回答
热心网友
时间:2023-11-13 12:47
这样就可以了,你判断null的那句没有起作用
import java.util.Calendar;
class CalendarList {
Integer year, month;
int dayNum, i, num, count = 0;
Calendar objCalendar;
int[] arrDayNum = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
CalendarList() {
objCalendar = Calendar.getInstance();
this.year = objCalendar.get(Calendar.YEAR);
this.month = objCalendar.get(Calendar.MONTH);
objCalendar.set(Calendar.DATE, 1);
}
CalendarList(String args0, String args1) {
this.month = Integer.parseInt(args0) - 1;
this.year = Integer.parseInt(args1);
objCalendar = Calendar.getInstance();
objCalendar.set(Calendar.YEAR, year);
objCalendar.set(Calendar.MONTH, month);
objCalendar.set(Calendar.DATE, 1);
}
void display() {
System.out.println("\t\t\t" + year + "年" + (month + 1) + "月");
System.out.println("日\t一\t二\t三\t四\t五\t六");
num = objCalendar.get(Calendar.DAY_OF_WEEK);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
arrDayNum[1] += 1;
}
for (i = 0; i < 12; i++) {
if (i == month) {
dayNum = arrDayNum[i];
break;
}
}
for (i = 1; i < num; i++) {
System.out.print(" \t");
count++;
}
for (i = 1; i <= dayNum; i++) {
System.out.print(i + "\t");
count++;
if (count == 7) {
System.out.println();
count = 0;
}
}
System.out.println();
}
}
public class MainClass {
public static void main(String[] args) {
try{
CalendarList objCalendar = new CalendarList(args[0], args[1]); //不输入参数,这里就说数组异常。
objCalendar.display(); }catch(Exception e){new CalendarList().display();}
}
}