Java 我的else语句中的代码已失效,我不知道';我不相信这是真的(爪哇)?

Java 我的else语句中的代码已失效,我不知道';我不相信这是真的(爪哇)?,java,if-statement,dead-code,Java,If Statement,Dead Code,我在java代码中使用有组织的BST。此函数/方法用于在树中搜索具有特定值的节点,并让用户知道该节点是否存在 void search(int item, Node root, int r, int c) { //if the integer is found if(root.val == item) { System.out.println("integer located at row: " + r + " & chil

我在java代码中使用有组织的BST。此函数/方法用于在树中搜索具有特定值的节点,并让用户知道该节点是否存在

    void search(int item, Node root, int r, int c) {

        //if the integer is found
        if(root.val == item) {
            System.out.println("integer located at row: " + r + " & child: " + c + "\n");
        }

        //if the integer is not found (use the closest value to find it)
        else if(root != null) {
            if(item < root.val) 
                search(item, root.left, r + 1, (c * 2) - 1);
            else
                search(item, root.right, r + 1, c * 2);
        }

        //if the root is a null (it doesn't exist or cannot be found)
        else {
            System.out.println("integer cannot be located\n");
        }
    }
void搜索(int项、节点根、int r、int c){
//如果找到整数
if(root.val==项){
System.out.println(“位于行:+r+”和子行:+c+”\n)的整数);
}
//如果找不到整数(使用最接近的值查找)
else if(root!=null){
如果(项目

问题在于最后的else语句。我的编译器说else语句中的任何内容都是死代码,这意味着它没有被使用。但是,如果函数遇到null并且找不到具有指定值的节点,我需要else语句中的代码。如果我将第二条else语句更改为
else if(root.val!=item&&root!=null)
,它就会消失,但这让我想知道是否存在root不等于null的点,我知道这应该是一种可能性。else语句真的是死代码吗?如果是,我如何更改它?

您的第一个
if
表达式:

if (root.val == item)
如果
root
null
则将抛出
NullPointerException
,否则将执行比较。因此,最后的
else
块永远无法执行

您可以尝试重新排序代码:

void search(int item, Node root, int r, int c) {
    if (root != null) {
       if (root.val == item) {
           System.out.println("integer located at row: " + r + " & child: " + c + "\n");
       } else if (item < root.val) {
            search(item, root.left, r + 1, (c * 2) - 1);
       } else {
            search(item, root.right, r + 1, c * 2);
       }
    } else {
       System.out.println("integer cannot be located\n");
    }
 }
void搜索(int项、节点根、int r、int c){
if(root!=null){
if(root.val==项){
System.out.println(“位于行:+r+”和子行:+c+”\n)的整数);
}else if(项
这是死代码,因为
root.val
中对
root
的取消引用要求root为非
null
。如果它是
null
,您将得到
NullPointerException

在我的IDE中,这是一个警告;代码在语法上是正确的,但从语义上讲,将永远不会输入最后的
else

要解决此问题,请先检查
if
语句中的
null

void search(int item, Node root, int r, int c) {
    if (root == null) {
        // if the root is a null (it doesn't exist or cannot be found)

        System.out.println("integer cannot be located\n");
    } else if (root.val == item) {
        // if the integer is found

        System.out.println("integer located at row: " + r + " & child: " + c + "\n");
    } else if (item < root.val) {
        // if the integer is not found (use the closest value to the left to find it)

        search(item, root.left, r + 1, (c * 2) - 1);
    } else {
        // if the integer is not found (use the closest value to the right find it)

        search(item, root.right, r + 1, c * 2);
    }
}
void搜索(int项、节点根、int r、int c){
if(root==null){
//如果根为空(不存在或找不到)
System.out.println(“找不到整数\n”);
}else if(root.val==项){
//如果找到整数
System.out.println(“位于行:+r+”和子行:+c+”\n)的整数);
}else if(项

请注意,您可以更改前两个
if
s,使其直接返回或停止方法的执行。然后,检查
不必位于
else
块中。
if
语句越浅越好(但始终对每个块使用大括号!)。

else if(root!=null)
越可疑。在
if
中,访问
root.val
。如果
root
null
,将抛出
NullPointerException
。因此,如果达到
,则将始终输入。因此,将永远不会输入最终的
else
。我假设外部
if
应该是外部
else if
中的第一个
if
,外部
else if
应该是
if
。如果root为null,则引用root.val将在第一个if中引发异常。因此,第一个else中的非空检查是不必要的。当遇到这样的问题时,第一个假设决不能是“数百万人使用的编译器肯定是错误的”。问题总是在你的代码中。简而言之,编译器非常聪明。如果(item
,为什么不
?我不明白为什么额外的块(除非保留OP的代码)是好的,我想我只是读了过去,它是在组织。我认为如果我用
>
替换
==
以获得对称性,然后最后处理找到的选项,那么它会更漂亮,但是是的,今天已经有足够的分支了。我忘记了
NullPointerException
。谢谢