本文へスキップ
BecomeCoder

Javaのエラー · エラー11

missing return statement ― 戻り値の無い分岐がある(コンパイル)

戻り値のある(void でない)メソッドで、すべての分岐が return するとは限らないと出ます。

出るコード

static int classify(int score) {
    if (score >= 60) {
        return 1;
    }
    // score が 60 未満のときに return が無い
}

エラーメッセージ

error: missing return statement

直し方

すべての分岐で値を返すようにするか、メソッドの最後に return を追加します。

static int classify(int score) {
    if (score >= 60) {
        return 1;
    }
    return 0;
}