我有以下方法:可以簡化這些「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.";
}
可用此法來簡化?
的回答你的問題是肯定的。 – rsp 2011-05-19 19:30:16
它可能會更好地命名! :P – 2011-05-19 19:31:33
@rsp當然可以,但是如何? – 2011-05-19 19:33:49