2011-02-23 64 views
-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); 
    } 
} 
+5

*我做了頌歌* ????? – 2011-02-23 11:11:17

+7

你真的應該使用更多的**描述性標題**。我們已經可以看到它是關於標籤中的Java,如果你不需要幫助,你就不會在這裏發佈吧? – 2011-02-23 11:11:26

+0

+1這個問題的描述方式是「代碼」有自己的想法! – zengr 2011-02-23 11:11:51

回答

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); 

但請注意一些已作出與問候重構你的代碼的其他意見,因爲已經提到,您的每一個setter方法在他們需要先固定的根本缺陷。

其他一般說明你的代碼:

在你setYear方法使用的是y的值更新對象的年份變量,但在第二,如果:

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

你實際上將y設置爲2011而不是year,所以這將不起作用。

出於某種原因,在你的setMonth方法中,你實際上並沒有設置月份,但是你只是驗證傳入的值,即如果值不在1和12之間,則返回1.因此,代碼不會' t匹配方法的名稱,你應該改變一個或另一個。

您的setDay方法與setMonth相同,因爲它實際上並沒有設置一天,只是驗證它。但更糟的是,對setDay的調用在很大程度上取決於已設置的月份和年份,因爲您使用monthyear變量來確定該日是否有效。這意味着setDay只能在setMonth和setYear之後調用,否則您將始終默認檢查0001年1月(因爲月份和年份默認設置爲1)。

+0

「您需要在創建後調用d1對象上的set方法,或者將參數傳遞給構造函數來設置它們。」我怎樣才能做到這一點? – Mugetsu 2011-02-23 11:49:59

+0

@ user630040請參閱我最近編輯的答案。 – DaveJohnston 2011-02-23 12:17:09

1

你的setter方法實際上應該設置一個字段並且什麼也不返回(void)。 setYear是可以的,但是你的兩個setter方法setMonth和setDay不會設置任何類字段並返回一個int值,這是沒有意義的。對你的問題是,哪個字段應該設置月更改,以及哪個字段應該更改?一旦你回答了這個問題,你就可以改變你的方法,使它們更好地工作。請評論,如果任何這沒有任何意義。

E.G.您的二傳手是像這樣:

// 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; 
    } 
} 

看到區別?

+0

我不知道如何使setDay void因爲返回1. – Mugetsu 2011-02-23 12:15:18

+0

同樣,不要返回任何東西。設置方法僅用於設置。見上面的編輯。 – 2011-02-23 12:37:07

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)); 
} 
+0

謝謝。 「您需要在創建後調用d1對象上的set方法,或者將參數傳遞給構造函數來設置它們。」我怎樣才能做到這一點? – Mugetsu 2011-02-23 11:56:54