为什么此代码缺少返回语句错误? [英] Why does this code have a missing return statement error?

查看:143
本文介绍了为什么此代码缺少返回语句错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将此代码的else部分更改为else语句,则运行时没有任何问题,因此我知道如何使其运行。我有点困惑的是为什么当它处于当前形式时我得到一个丢失的return语句错误。我的返回依赖于负的布尔变量的值。我覆盖了真假状态,还不足以涵盖所有内容吗?

If I change the else if portion of this code to an else statement it runs without any problems so I get how to make it run. What I'm a little confused about is why I get a missing return statement error when it is in its current form. My return is dependent on the value of the boolean variable of negative. I covered the true and false states, isn't that good enough to cover everything?

或者是我总是必须在其他地方或添加一个无意义的返回true到我的函数的底部,编译器接受我的代码覆盖每个案例?

Or is it that I always have to have a return statement within an else or to add a meaningless return true to the bottom of my function for the compiler to accept my code as covering every case?

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public boolean posNeg(int a, int b, boolean negative) {
      if (!negative) {
        return (a < 0 && b > 0) || (a > 0 && b < 0);
      }
      else if (negative) {
        return (a < 0 && b < 0);
      }
    }

    public static void main (String[] args) throws java.lang.Exception
    {
    }
}


推荐答案

当编译器看到 else时没有 else 或尾随的return语句,不能确定所有控制路径都会导致有效的return语句。

When the compiler sees else if without an else or a trailing return statement, it cannot be certain that all control paths will lead to a valid return statement.

编译器有时可能很聪明,但在这种情况下它不能很聪明(也不应该)。

The compiler can be smart at times, but it can't be smart in this situation (nor should it be).

此行为在您的示例中很有用:在这种情况下,您绝对没有理由使用 else 。一个简单的 else 更容易阅读,更简洁,更不容易出错。

This behavior is helpful in your example: there's absolutely no reason for you to use an else if in this situation. A simple else is easier to read, more concise, and less error prone.

一个在这种情况下,非常具有表现力。这意味着与if子句相反,如果代码将来发生变化,情况仍然如此。

An else is very expressive in this case. It means "the opposite of the if clause" which will still be the case if the code changes in the future.

如果编译器允许,您当前的代码更可能包含和/或引入错误。

Your current code would be more likely to contain and/or introduce a bug if the compiler allowed it.

以下是重写方法体的方法:

Here's how I would rewrite the method body:

if (negative) {
    return (a < 0 && b < 0);
}
else {
    return (a < 0 && b > 0) || (a > 0 && b < 0);
}

一般来说,你应该更喜欢 if(negative) over if(!negative)除非有令人信服的理由(即可读性)否则。

In general you should prefer if (negative) over if (!negative) unless there's a compelling reason (ie readability) to do otherwise.

此外,很多人(包括我自己)都试图将最简单的子句放在 if / else 语句中。使代码易于阅读是一件好事。

Also, a lot of people (including myself) try to put the most simple clause first in an if/else statement. Making your code easy to read is a good thing.

查看StephenC的答案,获得技术解释以及有关编译器为何如此行为的更多背景信息。

Check out StephenC's answer for a technical explanation and more background about why the compiler behaves this way.

这篇关于为什么此代码缺少返回语句错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆