2013-02-10 212 views
-2

我得到了一個基於用戶輸入定義季節的方法。 例如:1/6 =冬季 它的工作原理,但似乎應該有一個更簡單的方法來做到這一點,而不是所有的代碼。有什麼建議麼?我怎樣才能縮短這一點?

public String getSeason() 
    { 
     String result = "UNKNOWN"; 

     if (month == 1 && day >= 1) 
     { 
      result = "WINTER"; 
     } 
     else if (month == 2 && day >= 1) 
     { 
      result = "WINTER"; 
     } 
     else if (month == 3 && day <= 20) 
     { 
      result = "WINTER"; 
     } 
     else if (month == 3 && day >= 21) 
     { 
      result = "SPRING"; 
     } 
     else if (month == 4 && day >= 1) 
     { 
      result = "SPRING"; 
     } 
     else if (month == 5 && day >= 1) 
     { 
      result = "SPRING"; 
     } 
     else if (month == 6 && day <= 20) 
     { 
      result = "SPRING"; 
     } 
     else if (month == 6 && day >= 21) 
     { 
      result = "SUMMER"; 
     } 
     else if (month == 7 && day >= 1) 
     { 
      result = "SUMMER"; 
     } 
     else if (month == 8 && day >= 1) 
     { 
      result = "SUMMER"; 
     } 
     else if (month == 9 && day <= 22) 
     { 
      result = "SUMMER"; 
     } 
     else if (month == 9 && day >= 23) 
     { 
      result = "FALL"; 
     } 
     else if (month == 10 && day >= 1) 
     { 
      result = "FALL"; 
     } 
     else if (month == 11 && day >= 1) 
     { 
      result = "FALL"; 
     } 
     else if (month == 12 && day <= 20) 
     { 
      result = "FALL"; 
     } 
     else if (month == 12 && day >= 21) 
     { 
      result = "FALL"; 
     } 
     return result; 
    } 
+2

只需將多個測試合併爲一個:'if(month <3 || month == 3 && day <= 20 || month == 12 && day> = 21){result ='WINTER'} else {...'等一些測試是不必要的,例如'month == 2 && day> = 1'。 – 2013-02-10 17:29:16

+0

對不起,我正在玩的代碼嘗試不同的選項,忘了改回那部分。 – SkyVar 2013-02-10 17:30:14

回答

2

您可以通過使用相同的結果扔掉不必要的day >= 1(還有什麼?難道是),並結合個月縮短:

if (month <= 2 || (month == 3 && day <= 20) || (month == 12 && day >= 21)) { 
    // Winter 
} else if (month <= 5 || (month == 6 && day <= 21)) { 
    // Spring 
} else if (month <= 8 || (month == 9 && day <= 22)) { 
    // Summer 
} else { 
    // Fall 
} 
4

使用switch

switch (month) { 
    case 1: case 2: /* Winter */; break; 
    case 3: if (day <= 20) {/* Winter */} else {/* Spring */} break; 
    case 4: case 5: /* Spring */; break; 
    case 6: if (day <= 21) {/* Spring */} else {/* Summer */} break; 
    // Continue the pattern... 
    default: /* Unknown */; break; 
} 

這比梯子if-else好得多,因爲它很簡單。聲明使程序免於「墮入」並執行每個案例。

0

下面是使用Java日曆類查看問題的另一種方法。例如(未經測試的代碼)由於您需要閏年檢測,因此它稍微複雜一些,但正如我所說的,以不同的方式來思考問題。

public String testSeason(int year, int month, int day) { 
    //month is 0 based! 

    int FIRST_DAY_OF_SPRING = 31 + 28 + 21; // might need leap year detection to be completely accurate. 
    int FRIRST_DAY_OF_SUMMER = FRST_DAY_OF_SPRING + 10 + 31 + 30 +31; 
// define FALL and WINTER similarly. 

    Calendar testDate = new Calendar(); 
    testDate.set(year,month,day); 

    if (testDate.get(Calendar.DAY_OF_YEAR) < FIRST_DAY_OF_SPRING) return "Winter"; 
    if (testDate.get(Calendar.DAY_OF_YEAR) < FIRST_DAY_OF_SUMMER) return "Spring"; 
// continue for rest of seasons. 
} 
+0

爲什麼要製作對象之類的東西,並且在可以用數字解決問題時引入所有這些開銷? – MathSquared 2013-07-23 20:53:46

相關問題