编译器说缺少返回语句但我已经有3

时间:2014-05-23 22:07:26

标签: java

这很奇怪。我的编译器说我错过了一个return语句,但我已经有3个。 这是我的代码:

public int tortoiseMoves()
{
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    }

    if (i >= 6 && i <= 8)
    {
        int slowplod = 1;
        return slowplod;
    }

    if (i >= 9 && i <= 10)
    {
        int slip = -6;
        return slip;
    }
}

7 个答案:

答案 0 :(得分:8)

您必须确保返回始终值。如果你的所有条件都失败了,你就不会退货。

修复方法是将if语句链接起来,因为它们是独占的,并使用else来捕获所有其他情况。

public int tortoiseMoves() {
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    }

    else if (i >= 6 && i <= 8)
    {
        int slowplod = 1;
        return slowplod;
    }

    else if (i >= 9 && i <= 10)
    {
        int slip = -6;
        return slip;
    }
    else {
        // return something or throw exception
        return 0;
    }
}

答案 1 :(得分:7)

必须至少有一个肯定会达到的返回语句,但代码中没有这样的语句

答案 2 :(得分:4)

您需要为代码中的每个可能情况添加一个return语句。可能是所有ifs计算到false并因此不执行return语句的情况。试试这个:

public int tortoiseMoves()
{
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    }

    if (i >= 6 && i <= 8)
    {
        int slowplod = 1;
        return slowplod;
    }

    if (i >= 9 && i <= 10)
    {
        int slip = -6;
        return slip;
    }

    return Something to return if none of the ifs is true
}

虽然您可以将代码更改为:

public int tortoiseMoves()
{
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    } else if (i >= 6 && i <= 8){
        int slowplod = 1;
        return slowplod;
    } else if (i >= 9 && i <= 10) {
        int slip = -6;
        return slip;
    } else {
        return Something if none of the ifs is true
    }
}

答案 3 :(得分:3)

一般来说,有两种方法可以避免这个问题:

默认回报:

public int tortoiseMoves()
{
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    }

    if (i >= 6 && i <= 8)
    {
        int slowplod = 1;
        return slowplod;
    }

    if (i >= 9 && i <= 10)
    {
        int slip = -6;
        return slip;
    }

    //If code reaches here, you know none of the other returns were called
    return someDefaultValue;
    // OR
    // throw new Exception("Invalid value generated by tGen(): " + i);

}

或if / else

public int tortoiseMoves()
{
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    }
    else if (i >= 6 && i <= 8)
    {
        int slowplod = 1;
        return slowplod;
    }
    else //Code is guaranteed to reach here and return if the previous if statements fail
    {
        int slip = -6;
        return slip;
        // OR
        // throw new Exception("Invalid value generated by tGen(): " + i);
    }
}

答案 4 :(得分:3)

对于给定的函数运行,只有一个if(...)语句可以为true。因此,使用if/if else/else结构可能会更清楚。您可以将returnthrow语句置于if结构之外,但我认为将其放在else中会使其更清晰。

public int tortoiseMoves() {
    int i = tGen();                         // random: 1 <= i <= 10
    if      (i >= 1 && i <= 5)  return 3;   // fastplod
    else if (i >= 6 && i <= 8)  return 1;   // slowplod
    else if (i >= 9 && i <= 10) return -6;  // slip
    else throw new IllegalStateException(); // this shouldn't happen!
}

答案 5 :(得分:2)

很简单,如果所有if语句都为false,则代码不知道返回什么内容!

我不知道这是否是一个更好的解决方案:

public int tortoiseMoves()
{
    switch (tGen())
    {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5: return 3; // slowplod
        case 6:
        case 7:
        case 8: return 1; // fastplod
        case 9:
        case 10: return -6; // slip
        default: return 0; // default value if not in range
    }
}

解决方案2(查找表)://最快不使用切换或分支使用if

public int tortoiseMoves()
{
    int[] moves = {3,3,3,3,3,1,1,1,-6,-6};
    return moves[tGen()-1];
}

小心ArrayOutOfBondException!

答案 6 :(得分:1)

警告:许多评论表明您的错误是由于没有告诉编译器如何处理所有条件。但是,重要的是要意识到即使用if / if else覆盖所有可能的条件,编译器仍然会抱怨。

public boolean isPositive(int x){
    if(x > 0)        return true;
    else if(x <= 0)  return false;
    // error: "This method must return a result of type boolean"
}

在这种情况下,编译器在技术上有足够的信息来确定上述语句之一必须为真,因为x ∈ ℤ = {x | x ≤ 0} ⋃ {x | x > 0}。但是,您仍然需要在return内或else结构之外的最后if

相关问题