【问题标题】:error: missing return statement despite there being a return statement [duplicate]错误:尽管有返回语句,但缺少返回语句[重复]
【发布时间】:2020-05-28 16:39:50
【问题描述】:

' 尽管有许多 return 语句根据用户的特定字符串输入返回一个值,但仍有此错误。

public int convertPosition(String pos)
    {
        if (pos.equals("dean"))
        {
            return 0;
        }
        else if (pos.equals("chair"))
        {
            return 1;
        }
        else if (pos.equals("professor"))
        {
            return 2;
        }
        else if (pos.equals("secretary"))
        {
            return 3;
        }
        else if (pos.equals("teaching"))
        {
            return 4;
        }
        else if(pos.equals("otherwise"))
        {
            return -1;
        }
    }

【问题讨论】:

  • 如果pos"FOO",你认为返回值是多少?请记住,即使您认为它永远不会具有该值,编译器也不知道这一点。此外,您可能错了,在将来的某个时候您更改调用者代码以传递一些新值。

标签: java compiler-errors return


【解决方案1】:

这是一个baaaad方法。

    else if(pos.equals("otherwise"))
    {
        return -1;
    }
    return -999;

【讨论】:

    猜你喜欢
    • 2013-03-21
    • 2016-01-04
    • 2013-04-27
    • 2021-05-27
    • 2014-01-09
    • 2013-10-27
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    相关资源
    最近更新 更多