我遇到了一个问题,我在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;
}
这一个不会引起任何问题。有人可以告诉我警告来自哪里吗?如果您需要更多信息,请告诉我。
因为它代表所有那些'if's的没有做任何事情,因为你已经有了一个','各一个。这意味着你的内部'for'循环**总是**在第一次迭代时返回true,意味着'列++'永远不会到达。这就是为什么你应该总是使用'{}',即使使用简单的循环/ ifs。 – JonK 2014-10-30 09:07:34
你应该考虑在你的if语句中使用'&&'操作符。 – jhamon 2014-10-30 09:09:32