我已完成第一部分的月份部分需要帮助

时间:2018-04-10 20:36:03

标签: java

本练习是编写和测试主应用于[{1}}主方法类的Java应用程序。除main方法外,此类还包含两个带有以下标题的SeasonClassification方法:

static

要表示有效日期,一对整数,月份和日期必须满足1≤月≤12和1≤day≤该月的天数。给定一对整数,月和日,public static boolean isValidDate(int month, int day) public static String findSeason(int month, int day) 将返回相应的季节 如果该对表示有效日期,则在下表中指定,否则为findSeason

"invalidDate”

这就是我做的:

package javaapplication4;

public class JavaApplication4 {

date season
December 21 - March 20 Winter
March 21 - June 20 Spring
June 21 - September 20 Summer
September 21 - December 20 Autumn

}

1 个答案:

答案 0 :(得分:0)

发现您的代码和逻辑存在一些问题。现在应该工作,但测试不同的输入以确保

   public static boolean isValidDate(int month, int day) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
            if (day > 0) {
                if (day < 32) {
                    return true;
                }
                if (month == 4 || month == 6 || month == 9 || month == 11)
                    if (day < 31)
                        return true;
                if (month == 2)
                    if (day < 29)
                        return true;
            }
        }
        return false;
    }

    public static String findSeason(int month, int day){
        if (!isValidDate(month, day))
            return "invalidDate";

        switch(month) {

        case 1: // january same season
        case 2: // february
            return "winter";
        case 3: // march
// season changes
            return (day <= 20 ? "winter" : "spring");
        case 4:// april
        case 5: //may
            return "spring";
        case 6: // june
//season changes
            return (day <= 20 ? "spring" : "summer");
        case 7: // july
        case 8: // august
            return "summer";
        case 9: // september
            return (day <= 20 ? "summer" : "autumn");
        case 10: // october
        case 11: // november
            return "autumn";
        case 12: //december
          return (day <= 20 ? "autumn" : "winter");
        default:
            return "invalidDate";
        }
    }