-3

ifデータに応じて異なる出力を書き込む必要があるステートメントを作成しました。の場合int y = 2000, m = 5, d = 06;は機能しますが、の場合は正しい値を出力しませんint y = 2889, m = 44, d = 16;

これは私のコードです。誰かが私が何が悪いのかを理解するのを手伝ってくれませんか。

public class Date1 {

    private int year = 1; // any year
    private int month = 1; // 1-12
    private int day = 1; // 1-31 based on month

    //method to set the year
    public void setYear(int y) {
        if (y <= 0) {
            System.out.println("That is too early");
            year = 1;
        }

        if (y > 2011) {
            System.out.println("That year hasn't happened yet!");
            y = 2011;
        } else {
            year = y;
        }
    }

    public int setMonth(int theMonth) {
        if ( theMonth > 0 && theMonth <= 12 ) { // validate month
            return theMonth;
        } else { // month is invalid 
            System.out.printf("Invalid month (%d) set to 1.", theMonth);
            return 1; // maintain object in consistent state
        } // end else   
    }

    public int setDay( int theDay) {
        int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        // check if day in range for month
        if ( theDay > 0 && theDay <= daysPerMonth[ month ] ) {
            return theDay;
        }

        // check for leap year
        if ( month == 2 && theDay == 29 && ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) ) {
            return theDay;
        }

        System.out.printf( "Invalid day (%d) set to 1.", theDay );
        return 1;  // maintain object in consistent state 
    }

    //method to return the year
    public int getYear() {
        return year; 
    }

    //method to return the month
    public int getMonth(){
        return month; 
    }

    //method to return the day 
    public int getDay(){
        return day; 
    }

    // return a String of the form year/month/day
    public String toUniversalStringTime() { 
        return String.format( "The date using a default constructor %d/%d/%d \n", getYear(), getMonth(), getDay() ); 
    } // end toUniversalStringTime
}

public class Date1Test {

    public static void main(String[] args) {        
        int y = 2000, m = 5, d = 06;        

        Date1 d1 = new Date1(); //create a new object

        System.out.println(d1.toUniversalStringTime()); //call toUniversalStringTime()

        System.out.printf("The date I created is %d/%d/%d \n", y , m , d);  
    }
}
4

3 に答える 3

4

setDay、setMonth、またはsetYearメソッドを呼び出しているコードのどこにも表示されないため、toUniversalStringTimeの呼び出しは常に出力されると思います。

"The date using a default constructor 111 \n"

次に、その呼び出しの後、y、m、およびdの値を使用して手動で再度印刷します。

"The date I created is 200056 \n"

作成後にd1オブジェクトのsetメソッドを呼び出すか、パラメーターをコンストラクターに渡して設定する必要があります。

d1.setYear(y);
d1.setMonth(m);
d1.setDay(d);

ただし、コードのリファクタリングに関して行われた他のコメントのいくつかに注意してください。前述のように、各セッターメソッドには基本的な欠陥があり、最初に修正する必要があります。

コードに関するその他の一般的な注意事項:

setYearメソッドでは、yの値を使用してオブジェクトの年変数を更新していますが、2番目のメソッドでは次の場合です。

if (y > 2011) {
    System.out.println("That year hasn't happened yet!");
    y = 2011;
}

実際にはyではなく2011に設定してyearいるため、これは効果がありません。

何らかの理由で、setMonthメソッドでは実際に月を設定していませんが、渡された値を検証しているだけです。つまり、値が1〜12でない場合は、1を返します。したがって、コードは名前と一致しません。メソッドのいずれかを変更する必要があります。

setDayメソッドは、実際には日を設定せず、検証するだけであるという点でsetMonthと同じです。ただし、ここでさらに悪いのは、setDayの呼び出しが、すでに設定されている月と年に大きく依存することです。これは、monthandyear変数を使用して、日が有効かどうかを判断するためです。つまり、setDayはsetMonthとsetYearの後にのみ呼び出す必要があります。そうしないと、常にデフォルトで0001年1月をチェックすることになります(月と年はデフォルトで1に設定されているため)。

于 2011-02-23T11:15:13.690 に答える
2

あなたの年の設定者は間違っています:

//method to set the year
public void setYear(int y){

   if (y <= 0)
   {
     System.out.println("That is too early");
     year = 1;
   }

   if (y > 2011)
   {
     System.out.println("That year hasn't happened yet!");
     y = 2011;
   }
   else //<-- problem, makes the next stamtement only be executed if y <= 2011
     year = y;
 }

最後のelseステートメントはあなたの問題だと思います。2011年より前の場合にのみ年が更新されます。しかし、ステートメントのためy = 2011に2011年に限定する必要があると思います。したがって、必要なのは削除することです。else

または簡単な方法でそれを書いてください:

public void setYear(int year) {
   this.year = Math.max(1,Math.min(2011,year));
}
于 2011-02-23T11:14:03.360 に答える
1

セッターメソッドは実際にフィールドを設定し、何も返さない(void)必要があります。setYearは問題ありませんが、setterメソッドの2つであるsetMonthとsetDayは、クラスフィールドを設定せず、値intを返します。これは意味がありません。あなたにとっての質問は、どのフィールドをsetMonthに変更する必要があり、どのフィールドをsetDayに変更する必要があるかということです。この質問に答えたら、メソッドを変更して、より適切に機能するようにすることができます。これのいずれかが意味をなさない場合はコメントしてください。

EGあなたのセッターは次のようなものです:

// assuming you have an int field foo that should be > 0 and <= 100
public int setFoo(int foo) {
  if (foo < 0) {
     return 0;
  } else if (foo > 100) {
    return 100;
  } else {
    return foo;
  }
}

代わりに次のようにする必要がある場合:

public void setFoo(int foo) {
  if (foo < 0) {
     this.foo = 0;
  } else if (foo > 100) {
    this.foo = 100;
  } else {
    this.foo = foo;
  }
}

違いを見ます?

于 2011-02-23T11:13:05.277 に答える