-2

我得到了一种根据用户输入定义季节的方法。例如:1/6 = Winter 它可以工作,但似乎应该有一种更简单的方法来做到这一点,而不是拥有所有这些代码。有什么建议么?

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

3 回答 3

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梯子好得多,因为它很简单。这些break;语句使程序不会“失败”并执行每个案例。

于 2013-02-10T17:39:50.567 回答
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
}
于 2013-02-10T17:32:58.793 回答
0

这是使用Java Calendar 类来看待问题的另一种方式。例如(未经测试的代码)它有点复杂,因为您需要检测闰年,但正如我所说,这是一种考虑问题的不同方式。

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.
}
于 2013-02-10T18:31:07.590 回答