Having return in every if & else if & else statements vs one return at the end of code

huangapple go评论68阅读模式
英文:

Having return in every if & else if & else statements vs one return at the end of code

问题

第一个代码中,对每个 if、else if 和 else 语句使用了返回语句,但是出现了一个错误。我认为我已经涵盖了所有可能的情况,但是却出现了以下错误。

错误:
缺少返回语句,位于第16行

造成这个错误的原因是什么?另外,有没有办法可以改进我的第二个代码?

第二个代码中,我成功地消除了这个错误。

以下是第一个代码的翻译:

public int caughtSpeeding(int speed, boolean isBirthday) {
    if (!isBirthday)
        if (speed <= 60)
            return 0;
        else if (speed <= 80)
            return 1;
        else 
            return 2;
    if (isBirthday)
        if (speed <= 65)
            return 0;
        else if (speed <= 85)
            return 1;
        else 
            return 2;
}

以下是第二个代码的翻译:

public int caughtSpeeding(int speed, boolean isBirthday) {
    int flag = 0;
    if (!isBirthday)
        if (speed <= 60)
            flag = 0;
        else if (speed <= 80)
            flag = 1;
        else 
            flag = 2;
    if (isBirthday)
        if (speed <= 65)
            flag = 0;
        else if (speed <= 85)
            flag = 1;
        else 
            flag = 2;
    return flag;
}
英文:

Hello I have a quick question about multiple return statements vs single return statement at the end of the code.

From the first code, used return statement for every if, else if, and else statments and it gave me an error. And I thought I covered every possible cases but it game me an error bellow.

ERROR :
missing return statement line:16

What causes this error? Also, are there any way that I can improve my second code?

public int caughtSpeeding(int speed, boolean isBirthday) {
    if (!isBirthday)
        if (speed &lt;= 60)
            return 0;
        else if (speed &lt;= 80)
            return 1;
        else 
            return 2;
    if (isBirthday)
        if (speed &lt;= 65)
            return 0;
        else if (speed &lt;= 85)
            return 1;
        else 
            return 2;
}

From this second code, I was able to get rid of the error.

public int caughtSpeeding(int speed, boolean isBirthday) {
    int flag = 0;
    if (!isBirthday)
        if (speed &lt;= 60)
            flag = 0;
        else if (speed &lt;= 80)
            flag = 1;
        else 
            flag = 2;
    if (isBirthday)
        if (speed &lt;= 65)
            flag = 0;
        else if (speed &lt;= 85)
            flag = 1;
        else 
            flag = 2;
        return flag;
}

答案1

得分: 2

在你的第一段代码中,你测试了 isBirthday!isBirthday。Java 并不聪明到足够去判断这个列表是否穷尽。而且通常你也不会这样写:你会使用 else

public int caughtSpeeding(int speed, boolean isBirthday) {
    if (!isBirthday) {
        if (speed <= 60)
            return 0;
        else if (speed <= 80)
            return 1;
        else 
            return 2;
    } else {
        if (speed <= 65)
            return 0;
        else if (speed <= 85)
            return 1;
        else 
            return 2;
    }
}

> 另外,有没有什么方法可以改进我的第二段代码?

有的:通过使用第一段代码。第二段代码使用了一个完全多余的变量。使用第一段代码更好。

实际上,因为第二段代码使用了一个变量,如果你忘记了某个情况,Java 不会警告你 — 它会静默地返回 0,因为这是 flag 的初始值。这将是一个 bug。在可能的情况下,编写不会导致 bug 但会触发编译错误的代码 — 这就是代码 1 所做的,因此产生了错误消息(在这种情况下是一个误报,但通常是正确的)。

英文:

In your first code you test isBirthday and !isBirthday. Java isn’t smart enough to see that this list is exhaustive. And you wouldn’t normally write it like that, anyway: you’d write else instead.

public int caughtSpeeding(int speed, boolean isBirthday) {
    if (!isBirthday) {
        if (speed &lt;= 60)
            return 0;
        else if (speed &lt;= 80)
            return 1;
        else 
            return 2;
    } else {
        if (speed &lt;= 65)
            return 0;
        else if (speed &lt;= 85)
            return 1;
        else 
            return 2;
    }
}

> Also, are there any way that I can improve my second code?

Yes: by using the first code. The second code uses a completely redundant variable. Use the first code.

In fact, because the second code uses a variable, Java won’t warn you if you forget a case — it will silently return 0, because that’s how flag was initialised. That would be a bug. When possible, write code that can’t lead to bugs but triggers compile errors instead — that’s what code 1 does, hence the error message (which was a false positive in this case, but which is in general correct).

答案2

得分: 2

好的,以下是翻译好的内容:

嗯,对我们来说很明显,“如果生日”和“如果不是生日”中的一个将被执行,但是Java编译器并不这样认为。

对它来说,如果第一个“如果”没有被执行,且第二个“如果”也没有被执行,那么在不执行“返回”的情况下,你会到达方法的末尾。

英文:

Well, it's sort-of obvious to us that one if "if birthday" and "if not birthday" is going to be executed, but the Java compiler doesn't see it that way.

To it, if the first 'if' does not get executed, and the second 'if' also does not get executed, then you come to the end of the method without executing a 'return'.

huangapple
  • 本文由 发表于 2020年10月26日 06:46:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64529592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定