【问题标题】:The method must return a type int该方法必须返回一个 int 类型
【发布时间】:2015-05-17 18:48:52
【问题描述】:
public int computeStyle(String season) {
    if(season.equals("summer")){
        if (this.style.equals("toque")){
            return 8;
        }
        if (this.style.equals("sun visor")){
            return 1;
        }
        if (this.style.equals("fedora")){
            return 6;
        }
    }
    else if(season.equals("winter")){
        if (this.style.equals("toque")){
            return 1;
        }
        if (this.style.equals("sun visor")){
            return 8;
        }
        if (this.style.equals("fedora")){
            return 7;
        }
    }
    else return 5;
}

为什么我总是收到方法必须返回类型 int 的错误。这个功能有什么问题?它应该在所有可能的情况下都返回一个 int 对吧?

【问题讨论】:

    标签: java function return


    【解决方案1】:

    有两条路径未涵盖:

    public int computeStyle(String season) {
        if(season.equals("summer")){
            if (this.style.equals("toque")){
                return 8;
            }
            if (this.style.equals("sun visor")){
                return 1;
            }
            if (this.style.equals("fedora")){
                return 6;
            }
            //here
        }
        else if(season.equals("winter")){
            if (this.style.equals("toque")){
                return 1;
            }
            if (this.style.equals("sun visor")){
                return 8;
            }
            if (this.style.equals("fedora")){
                return 7;
            }
            //here
        }
        else return 5;
    }
    

    解决方法:用默认返回值声明一个变量并正确赋值:

    public int computeStyle(String season) {
        int result = 5;
        if(season.equals("summer")){
            if (this.style.equals("toque")){
                result = 8;
            }
            if (this.style.equals("sun visor")){
                result = 1;
            }
            if (this.style.equals("fedora")){
                result = 6;
            }
        }
        else if(season.equals("winter")){
            if (this.style.equals("toque")){
                result = 1;
            }
            if (this.style.equals("sun visor")){
                result = 8;
            }
            if (this.style.equals("fedora")){
                result = 7;
            }
        }
        return result;
    }
    

    【讨论】:

    • 或者你可以简单地将else return 5;替换为return 5;
    • @Asaph 或停止这种废话并使用单点返回而不是多个return 语句。此外,可以重构代码以使其更具可读性和更易于维护,但这超出了本 Q/A 的范围。
    • 有趣。我刚刚在stackoverflow.com/q/30290577 中讨论过这个问题。随意发表意见。
    【解决方案2】:

    如果您的返回类型是 int,那么您的方法必须如何返回 int。

    在这种情况下,在您的外部if else 内部,您仍然有if 块,这意味着如果在外部if else 内部,如果满足条件,则它不会返回任何内容。

    在这种情况下,您应该始终在末尾添加一个 return 语句。

    像这样:

    public int computeStyle(String season) {
        if(season.equals("summer")){
            if (this.style.equals("toque")){
                return 8;
            }
            if (this.style.equals("sun visor")){
                return 1;
            }
            if (this.style.equals("fedora")){
                return 6;
            }
        }
        else if(season.equals("winter")){
            if (this.style.equals("toque")){
                return 1;
            }
            if (this.style.equals("sun visor")){
                return 8;
            }
            if (this.style.equals("fedora")){
                return 7;
            }
        }
        else return 5;
    
       // If everything fails, then it ll return 0 at least
       return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      • 2012-08-13
      • 2017-02-25
      • 2018-03-08
      相关资源
      最近更新 更多