2014-10-30 85 views
5

我遇到了一个问题,我在Eclipse中遇到了“死代码”警告,我真的不知道为什么。代码来自我的Connect Four项目,更确切地说,它是从Class中检查是否有人获胜。该方法检查所有水平获胜可能性为红色。代码如下:死码从哪里来?

/** 
* 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's的没有做任何事情,因为你已经有了一个','各一个。这意味着你的内部'for'循环**总是**在第一次迭代时返回true,意味着'列++'永远不会到达。这就是为什么你应该总是使用'{}',即使使用简单的循环/ ifs。 – JonK 2014-10-30 09:07:34

+1

你应该考虑在你的if语句中使用'&&'操作符。 – jhamon 2014-10-30 09:09:32

回答

1

你函数中的死代码是你内循环的the increment statementcolumn++)。总是会执行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; 
} 
+0

好的,你的答案更详细,所以我会接受这个。感谢所有对我真正愚蠢的问题的答案! – Lunaetic 2014-10-30 09:16:17

0

在上面的方法中,在每个if语句之后,作为你的第二个方法是正确的,哪个是正确的方法。

if(gw.buttons[line][column].getIcon().equals(gw.red)); <-- 

终止,如果没有它的自我。您的代码行相当于

if(condition) 
    { 

    } 

这意味着if条件死后的代码。

+1

我不知道为什么我这样做,我怎么没有看到...非常感谢! – Lunaetic 2014-10-30 09:08:35

+1

@ifLoop当然它! Downvote绝对不必要 – 2014-10-30 09:14:06

+0

@ifLoop尽管我没有提到'dead'这个词,但答案是自我解释。编辑了一下澄清。 – 2014-10-30 09:16:19

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语句用分号。