0

我在这里寻找类似的东西,但似乎没有任何帮助。我正在尝试将输入到我的布尔方法中的值返回给我的测试程序。任何建议都会被排除在外。由于我的代码很长,我只会包含我认为相关的内容。

    public class SeasonCalculatorTester
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the month: ");
        int month = in.nextInt();

        System.out.print("Enter the day: ");
        int day = in.nextInt();

        SeasonCalculator calculator = new SeasonCalculator(month, day);

        //TODO USE THE calculator (SeasonCalculator) TO CHECK FOR VALID MONTH AND VALID DAY.  
        //     IF MONTH OR DAY IS NOT VALID PRINT ERROR MESSAGE.  
        //     IF VALID PRINT OUT THE MONTH (IN STRING FORM, NOT INT FORM), THE DAY,
        //     AND THE SEASON THAT THE DAY IS IN 

        if(calculator.isValidMonth())
        {
            System.out.print(calculator.getMonth());
        }
        if(calculator.isValidDay())
        {
            System.out.print(" "+calculator.isValidDay()+" is in the "+ calculator.getSeason());
        }       
        else
        {
            System.out.println("False");
        }

    }
}

我需要返回 int 值的方法是“isValidDay()”,代码如下:

public boolean isValidDay()
    {
        if ((month == 1 && day >= 1) && (month == 1 && day <= 31))
        {
            return true;
        }
        if ((month == 2 && day >= 1) && (month == 2 && day <= 29))
        {
            return true;
        }
        if ((month == 3 && day >= 1) && (month == 3 && day <= 31))
        {
            return true;
        }
        if ((month == 4 && day >= 1) && (month == 4 && day <= 30))
        {
            return true;
        }
        if ((month == 5 && day >= 1) && (month == 5 && day <= 31))
        {
            return true;
        }
        if ((month == 6 && day >= 1) && (month == 6 && day <= 30))
        {
            return true;
        }
        if ((month == 7 && day >= 1) && (month == 7 && day <= 31))
        {
            return true;
        }
        if ((month == 8 && day >= 1) && (month == 8 && day <= 31))
        {
            return true;
        }
        if ((month == 9 && day >= 1) && (month == 9 && day <= 30))
        {
            return true;
        }
        if ((month == 10 && day >= 1) && (month == 10 && day <= 31))
        {
            return true;
        }
        if ((month == 11 && day >= 1) && (month == 11 && day <= 31))
        {
            return true;
        }
        if ((month == 12 && day >= 1) && (month == 12 && day <= 31))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
4

4 回答 4

2

你可以做的是:

  • 如果参数无效,让您的构造函数抛出异常
  • 通过将月份的最大天数存储在数组中来检查日期是否有效
  • 提供获取器来访问月/日
class SeasonCalculator {
    private static final int[] days = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    private final int month;
    private final int day;

    public SeasonCalculator(int month, int day) {
        if (month < 1 || month > 12) {
            throw new IllegalArgumentException("Not a valid month: " + month);
        }
        if (day < 1 || day > days[month - 1]) {
            throw new IllegalArgumentException("Not a valid day: " + day + " for month " + month);
        }
        this.month = month;
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }
}
于 2013-02-09T19:08:08.673 回答
0

添加一个不同的方法SeasonCalculator,只返回day. 然后:

if(calculator.isValidDay())
{
    System.out.print(" "+calculator.getDay()+" is in the "+ calculator.getSeason());
}
于 2013-02-09T17:11:13.893 回答
0

您不能int从返回布尔值的方法返回 a 或某物。您可以添加一个全局变量,您可以在日期有效时从方法中设置该变量。

于 2013-02-09T19:29:49.720 回答
0

您也可以尝试返回一个 Integer 对象。如果为空,则不正常。

public static Integer returnInt() {
    Integer ret = calculate_something();
    if (ret > AN_OK_CONDITION) {
        return ret;
    }
    else {
        return null;
    }
}

然后,调用它:

Integer ret = returnInt();
    if (ret != null) {
        System.out.println("Number: " + ret);
    }
于 2013-02-09T21:02:11.030 回答