我得到了一个基于用户输入定义季节的方法。 例如: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;
}
只需将多个测试合并为一个:'if(month <3 || month == 3 && day <= 20 || month == 12 && day> = 21){result ='WINTER'} else {...'等一些测试是不必要的,例如'month == 2 && day> = 1'。 – 2013-02-10 17:29:16
对不起,我正在玩的代码尝试不同的选项,忘了改回那部分。 – SkyVar 2013-02-10 17:30:14