死代码是从哪里来的?

5

我有一个问题,在Eclipse中出现了“死代码”警告,但我真的不知道为什么。这段代码来自于我的四子棋项目,更准确地说是检查是否有人获胜的类。该方法检查所有红色水平获胜的可能性。代码如下:

/**
 * Method to check the horizontal winning possibilities for red
 * @return true if red won or false if not
 */
public boolean checkHorRed(){
    for(int line = 0; line < 6; line++) {
        for(int column = 0; column < 4; column++) { //column++ is underlined and causes the "dead Code" warning
            if(gw.buttons[line][column].getIcon().equals(gw.red));
                if(gw.buttons[line][column+1].getIcon().equals(gw.red));
                    if(gw.buttons[line][column+2].getIcon().equals(gw.red));
                        if(gw.buttons[line][column+3].getIcon().equals(gw.red));
                            return true;
        }
    }
    return false;
}    

游戏因此方法而立即获胜,这很奇怪的是,类中其他看起来几乎相同的方法没有任何问题。以下是检查黄色垂直获胜可能性的方法,供对比:
/**
 * Method to check the vertical winning possibilities for yellow
 * @return true or false
 */
public boolean checkVertYel(){
    for(int line = 3; line < 6; line++) {
        for(int column = 0; column < 7; column++) {
            if(gw.buttons[line][column].getIcon().equals(gw.yellow))
                if(gw.buttons[line-1][column].getIcon().equals(gw.yellow))
                    if(gw.buttons[line-2][column].getIcon().equals(gw.yellow))
                        if(gw.buttons[line-3][column].getIcon().equals(gw.yellow))
                            return true;
        }
    }
    return false;
}    

这个没有引起任何问题。有人能告诉我警告来自哪里吗?如果需要额外的信息,请告诉我。


1
就目前而言,所有这些 if 都没有起到任何作用,因为每个 if 后面都有一个 ;。这意味着你的内部 for 循环在第一次迭代时始终返回 true - 这意味着 column++ 永远无法被执行。这就是为什么即使是简单的循环/条件语句,也应该始终使用 {} 的原因。 - JonK
1
你应该在if语句中考虑使用&&运算符。 - jhamon
3个回答

1
你的函数中存在死代码,即内部for循环的增量语句 (column++)。如果执行了循环,return true语句将始终被执行,因此循环增量永远不会发生。
以下是格式正确的代码:
// ...

for(int column = 0; column < 4; column++) {
    //column++ is underlined and causes the "dead Code" warning
    if(gw.buttons[line][column].getIcon().equals(gw.red));

    if(gw.buttons[line][column+1].getIcon().equals(gw.red));

    if(gw.buttons[line][column+2].getIcon().equals(gw.red));

    if(gw.buttons[line][column+3].getIcon().equals(gw.red));

    return true;
}

// ...

你可以轻松地发现错误:return true将始终被执行,因此内部循环的增量语句将不会被执行。
你的代码应该像这样:
public boolean checkHorRed() {
    for(int line = 0; line < 6; line++) {
        for(int column = 0; column < 4; column++) {
            //column++ is underlined and causes the "dead Code" warning
            if(gw.buttons[line][column].getIcon().equals(gw.red)
                    && gw.buttons[line][column+1].getIcon().equals(gw.red)
                    && gw.buttons[line][column+2].getIcon().equals(gw.red)
                    && gw.buttons[line][column+3].getIcon().equals(gw.red) {
                return true;
            }
        }
    }

    return false;
}

好的,你的回答更加详细,我会接受这个答案。感谢对我这个非常愚蠢的问题的所有回答! - Lunaetic

0
在上面的方法中,你在每个 if 语句后面都有一个分号,而你的第二个方法是正确的,这是正确的方式。
  if(gw.buttons[line][column].getIcon().equals(gw.red)); <--

这将终止if语句本身。您的代码行相当于

   if(condition)
    {

    }

这意味着 if 条件语句后面的代码不会被执行。

1
我不知道为什么我会这样做,也不知道我怎么没看到那个问题... 非常感谢! - Lunaetic
@if循环 虽然我没有提到“死循环”这个术语,但答案是不言自明的。稍作编辑以澄清。 - Suresh Atta
1
@Lunaetic 没问题。每个程序员都会遇到这种情况。愉快的编程。 - Suresh Atta

0

这是您重新格式化后的代码:

public boolean checkHorRed() {
    for (int line = 0; line < 6; line++) {
        for (int column = 0; column < 4; column++) { //column++ is underlined and causes the "dead Code" warning
            if (gw.buttons[line][column].getIcon().equals(gw.red)) {
                ;
            }
            if (gw.buttons[line][column + 1].getIcon().equals(gw.red)) {
                ;
            }
            if (gw.buttons[line][column + 2].getIcon().equals(gw.red)) {
                ;
            }
            if (gw.buttons[line][column + 3].getIcon().equals(gw.red)) {
                ;
            }
            return true; //this will always happen
        }
    }
    return false;
}

这是另一个:

public boolean checkVertYel() {
    for (int line = 3; line < 6; line++) {
        for (int column = 0; column < 7; column++) {
            if (gw.buttons[line][column].getIcon().equals(gw.yellow)) {
                if (gw.buttons[line - 1][column].getIcon().equals(gw.yellow)) {
                    if (gw.buttons[line - 2][column].getIcon().equals(gw.yellow)) {
                        if (gw.buttons[line - 3][column].getIcon().equals(gw.yellow)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}

基本上,你不应该在if语句末尾加分号。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接