Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何避免;“无法到达的语句”;及;否则不带if“;在爪哇?_Java_If Statement_Unreachable Statement - Fatal编程技术网

Java 如何避免;“无法到达的语句”;及;否则不带if“;在爪哇?

Java 如何避免;“无法到达的语句”;及;否则不带if“;在爪哇?,java,if-statement,unreachable-statement,Java,If Statement,Unreachable Statement,所以我的代码是用来比较机器人的y坐标和目标的y坐标。在中添加print语句时,使函数返回任何内容时遇到问题。我有一种感觉,这与括号有关,但我不确定如何使用它们 这不是整个程序,但它是其中唯一有错误的部分 当我试图编译此文件时: public class Ex12 { private byte isTargetNorth(IRobot robot) { if (robot.getLocationY() > robot.getTargetLocation(

所以我的代码是用来比较机器人的y坐标和目标的y坐标。在中添加print语句时,使函数返回任何内容时遇到问题。我有一种感觉,这与括号有关,但我不确定如何使用它们

这不是整个程序,但它是其中唯一有错误的部分

当我试图编译此文件时:

public class Ex12 
{

    private byte isTargetNorth(IRobot robot)
    {

        if (robot.getLocationY() > robot.getTargetLocation().y) {

            return 1; System.out.println("north");

        } else if (robot.getLocationY() == robot.getTargetLocation().y) {

            return 0; System.out.println("no");

        } else { 

            return -1; System.out.println("south");

        }  

    }
}
我得到错误:无法到达语句

当我尝试这个:

public class Ex12 
{

    private byte isTargetNorth(IRobot robot)
    {

        if (robot.getLocationY() > robot.getTargetLocation().y)

            return 1; System.out.println("north");

        else if (robot.getLocationY() == robot.getTargetLocation().y)

            return 0; System.out.println("no");

        else

            return -1; System.out.println("south");

    }
}
我得到错误:“else”没有“if”


删除System.out.println()函数时,我没有收到任何错误。

返回语句退出您的方法。因此,永远不会调用System.out。

第一个:将return语句移动到它们各自的
System.out.println
调用之后-return退出当前方法,因此
System.out.println
永远不会被调用,因此无法访问

第二个问题是格式混乱:你的代码

if (robot.getLocationY() > robot.getTargetLocation().y)
    return 1; System.out.println("north");
else if ...
//...
实际上相当于

if (robot.getLocationY() > robot.getTargetLocation().y) {
    return 1;
}
System.out.println("north");
else if ... //else without if right here, because else should follow immediately after an if block!

没有if的else示例很好地提醒了您为什么在省略大括号时要格外小心。

在您的第一个代码块中,在return语句之后有System.out.println,这使它们无法访问。如果将System.out.println放在return语句前面,它就会工作

在第二个示例中,从if语句中删除了块({…}),这意味着每个条件只能有一条语句。您有两个return和System.out.println。

您写道:

return 1;
System.out.println(""); // << This is written after you return to the calling method.
要解决您的问题:

  • 不要在返回值后编写代码
  • 写括号
    {}
    Mor来写,但是你总能看到块

  • 除了明显的问题外,名为
    isTargetNorth
    的方法应该返回类型为
    boolean
    的值,而不是
    byte
    。消息的哪一部分你不明白?@RohitJain:这不是最糟糕的错误,不是吗?在返回之前打印消息,返回后(不使用try/finally)您不能做任何事情@JonSkeet Yes。真正的问题已经得到了回答。:)
    if() Some Code;
    // This statement will be executed, because  it is outside of the if-block
    else 
        // The else above has no if.