Code Fix

初級missing return statement の原因と直し方 › パターン5

missing return statement の原因と直し方

すべての経路で値を返す必要があります。

 1  public class Main {
 2      static String label(int n) {
 3          switch (n) {
 4              case 1:
 5                  return "one";
 6              default:
 7                  ______________________
                               ^
 8          }
 9      }
10      public static void main(String[] args) {
11          System.out.println(label(2));
12      }
13  }
Main.java:9: error: missing return statement

空欄を埋めてビルドを通す

広告
広告スロット(未設定)

missing return statement の原因と直し方

ifだけで分岐したときに漏れる道を可視化します。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)