-2

これはかなり単純なことですが、境界外の例外が発生し続ける理由がわかりません。さまざまな方法で配列を初期化しようとしましたが、それでも解決しません。toString メソッドと、その日が定義された月の正しい範囲内にあるかどうかを確認する部分で例外が発生します。

public class MyDate 
{
//variables for year day and month.
private int year, day, month;

//variable for n which is used to compare to the days for leap years.
int n;

String[] Months = {"January","Feburary","March","April","May","June","July"
                ,"August" ,"September","October","November","December"};
int[] MaxDays = {31,31,31,30,31,30,31,31,30,31,30,31};

public MyDate(int year, int day, int month)
{      
    //Checks to see if the year is valid.
    if (year < 1600 || year > 3000)
    {
        System.out.println("The year entered is not valid, "
                + "You entered: " + year);
        System.out.println("The year must be between 1600 and"
                + " 3000.");
        System.exit(0);
    }
    //Checks to see if the month is valid.
    if (month < 1 || month > 12)
    {
        System.out.println("The month is not valid, " + "You Entered: "
                + month);
        System.out.println("The month must be between 1 and 12.");
        System.exit(0);
    }

    //Checks to see if the day is valid
    if (day >= MaxDays[this.day - 1])
    {
        advance();
    }
    //Checks for a leap year, and if the day is valid or not.
    if ((year %  400 == 0) || (year % 100 != 0))
    {   
           if (year % 4 == 0)
            {
                if (month == 2)
                {   
                    //Loops that goes from 27-31 comparing the day to n.

                    int n = 27;
                    do
                    {
                        if (n == day)
                        {
                            System.out.println("This is a leap year, and you entered " + n + " as the day"
                                    + "\n" + "this day is not valid for a leap year");
                            System.out.println();
                            break;
                        }

                        if (n == 32)
                        {
                            break;
                        }
                        n++;
                    }
                    while (n == day);

                    }
                }
    }
    else    
    {
        this.year=year;
        this.day=day;
        this.month=month;
    }
}

//Checks to see if two dates are equal to eachother.
public boolean equals(Object obj)
{
    if (obj instanceof MyDate){
        MyDate d = (MyDate)obj;
        if (this.getMonth()==d.getMonth() && this.getDay()==d.getDay() &&
            this.getYear()==d.getYear())
            return true;
        else
            return false;
    }
    return false;
}

//gives a general format for the month day and year.
public String toString()
{
    return Months[month - 1] + " " + day + "," + year + "";
} 
4

2 に答える 2

1

問題はそこから来ているようです

if (day >= MaxDays[this.day - 1])

どこthis.day = 0

代わりにあなたが合格するべきだと思いますmonth

if (day >= MaxDays[month - 1])

更新
あなたのtoString()方法でmonth= 0は、前の月を設定しなかったためです。コンストラクター内のブロックの外側で行い
ます。this.month=month;else

于 2013-09-12T02:54:37.963 に答える
0

おそらくエラーはここにありますif condition

 if (day >= MaxDays[this.day - 1])

MaxDays[] は 12 要素の配列です - インデックス 0 ~ 11

days can be from 1-31,もしそうならday is greater than 12, it will give an IndexOutOfBoundException.

ただし、コードでは、使用する前に初期化していないため、常にゼロになります。

于 2013-09-12T02:53:20.967 に答える