如何从布尔方法返回整数?

时间:2013-02-09 17:08:51

标签: java

我在这里找到了类似的东西,但似乎没有任何帮助。我试图将我的值输入到我的测试程序的布尔方法中。任何建议都将被大大排除。由于我的代码太长,我只会包含我认为相关的代码。

    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 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

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

if(calculator.isValidDay())
{
    System.out.print(" "+calculator.getDay()+" is in the "+ calculator.getSeason());
}

答案 2 :(得分:0)

您无法从返回布尔值的方法返回int或其他内容。您可以添加一个全局变量,您可以在日期有效时从该方法设置该变量。

答案 3 :(得分:0)

您也可以尝试返回Integer对象。 如果它为null,那就不行了。

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