2011-05-19 191 views
3

我有以下方法:可以簡化這些「if語句」嗎?

public static String format_String(int hours, int minutes, int seconds) 
{ 
    if(hours > 0 && minutes > 0 && seconds > 0) return hours + " hours, " + minutes + " minutes and " + seconds + " seconds."; 
    else if(hours > 0 && minutes > 0 && seconds == 0) return hours + " hours and " + minutes + " minutes."; 
    else if(hours > 0 && minutes == 0 && seconds > 0) return hours + " hours and " + seconds + " seconds."; 
    else if(hours > 0 && minutes == 0 && seconds == 0) return hours + " hours."; 
    else if(hours == 0 && minutes > 0 && seconds > 0) return minutes + " minutes and " + seconds + " seconds."; 
    else if(hours == 0 && minutes > 0 && seconds == 0) return minutes + " minutes."; 
    else //if(hours == 0 && minutes == 0 && seconds > 0) 
    return seconds + " seconds."; 
} 

可用此法來簡化?

+7

的回答你的問題是肯定的。 – rsp 2011-05-19 19:30:16

+0

它可能會更好地命名! :P – 2011-05-19 19:31:33

+0

@rsp當然可以,但是如何? – 2011-05-19 19:33:49

回答

4

其中最棘手的部分是是否將部件與" and "","分開,這取決於您當前正在打印的部件的右側顯示多少非零部件。其餘的(打印姓名和號碼)很簡單。

因此,您可以通過從右到左構建字符串來減少分支的數量。

public static String format_String(int hours, int minutes, int seconds) 
{ 
    StringBuilder result = new StringBuilder("."); 
    String sep = "", nextSep = " and "; 
    if (seconds > 0) { 
     result.insert(0, " seconds").insert(0, seconds); 
     sep = nextSep; 
     nextSep = ", "; 
    } 
    if (minutes > 0) { 
     result.insert(0, sep).insert(0, " minutes").insert(0, minutes); 
     sep = nextSep; 
     nextSep = ", "; 
    } 
    if (hours > 0) { 
     result.insert(0, sep).insert(0, " hours").insert(0, hours); 
    } 
    return result.toString(); 
} 

或者更一般地說:

public static String formatString(SortedMap<TimeUnit, Integer> parts) { 
    StringBuilder result = new StringBuilder("."); 
    String sep = "", nextSep = " and "; 
    for (Map.Entry<TimeUnit, Integer> e: parts.entrySet()) { 
     TimeUnit field = e.getKey(); 
     Integer quantity = e.getValue(); 
     if (quantity > 0) { 
      result.insert(0, sep) 
        .insert(0, field.toString().toLowerCase()) 
        .insert(0, ' ') 
        .insert(0, quantity); 
      sep = nextSep; 
      nextSep = ", "; 
     } 
    } 
    return result.toString(); 
} 
+0

謝謝@finnw :) – 2011-05-19 20:08:29

2

想想你將如何準備說相同的句子。

if hours > 0 then say the hours 
if minutes > 0 then say the minutes 
if seconds > 0 then say the seconds 

然後使用帶有單個返回的StringBuilder創建邏輯。

+0

您仍然需要插入標點符號。 – finnw 2011-05-19 19:52:25

+0

有趣的部分留給讀者練習;-) – 2011-05-19 20:44:49

1

您可以使用三元運算,使其少一些冗長:

return 
(hours > 0 && minutes > 0 && seconds > 0) ? hours + " hours, " + minutes + " minutes and " + seconds + " seconds." : 
(hours > 0 && minutes > 0 && seconds == 0) ? hours + " hours and " + minutes + " minutes." : 
(hours > 0 && minutes == 0 && seconds > 0) ? hours + " hours and " + seconds + " seconds." : 
(hours > 0 && minutes == 0 && seconds == 0) ? hours + " hours." : 
(hours == 0 && minutes > 0 && seconds > 0) ? minutes + " minutes and " + seconds + " seconds." : 
(hours == 0 && minutes > 0 && seconds == 0) ? minutes + " minutes." : 
seconds + " seconds."; 
+0

+1我喜歡它:) – 2011-05-19 19:40:54

0

假設所有值永遠是> = 0的代碼看起來比較複雜,但也有許多比較少!

 
if(hours > 0) { 
    if(minutes > 0) { 
     if(seconds > 0) return hours + " hours, " + minutes + " minutes and " + seconds + " seconds."; 
     return hours + " hours and " + minutes + " minutes."; 
    } 
    if(seconds > 0) return hours + " hours and " + seconds + " seconds."; 
    return hours + " hours."; 
} 
if(minutes > 0) { 
    if(seconds > 0) return minutes + " minutes and " + seconds + " seconds."; 
    return minutes + " minutes."; 
} 
return seconds + " seconds."; 
1

這個怎麼樣?

public static String formatTime(int hours, int minutes, int seconds) { 
    List<String> parts = new ArrayList<String>(3); 
    if (hours > 0) parts.add(hours + " hours"); 
    if (minutes > 0) parts.add(minutes + " minutes"); 
    if (parts.isEmpty() || seconds > 0) parts.add(seconds + " seconds"); 
    StringBuilder builder = new StringBuilder(); 

    for (int i = 0; i < parts.size(); i++) { 
     if (i > 0) builder.append((i < parts.size() - 1) ? ", " : " and "); 
     builder.append(parts.get(i)); 
    } 

    return builder.append(".").toString(); 
} 

它更具可擴展性。我只問它是否可以在新開發的Java開發人員中讀取。

+1

似乎它更具可擴展性......你可以擴大這個包括天,周,月等。 – Michael 2011-05-19 19:51:22

+0

這並不容易閱讀,但也沒有任何其他解決方案。 – finnw 2011-05-19 19:51:31

+0

@BalusC您可以通過將ArrayList的大小初始化爲3來進一步優化。 – Michael 2011-05-19 19:56:38