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ブール値を返すメソッドからまたは何かを返すことはできません。日付が有効なときにメソッドから設定できるグローバル変数を追加できます。

于 2013-02-09T19:29:49.720 に答える
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);
    }
于 2013-02-09T21:02:11.030 に答える