当板上的所有图块都= = null时,此代码应该关闭应用程序,但是当循环运行时,它只要找到1个为null的图块就会退出应用程序。我该如何解决这个问题?
谢谢!
public void close(){
for (int row = 0; row < 4; row++){
for (int col = 0; col < 4; col++){
if (tiles[row][col] == null) {
System.exit(0);
}
}
}
}
答案 0 :(得分:3)
因为你的逻辑错了。它应该像以下一样。
public void close(){
for (int row = 0; row < 4; row++){
for (int col = 0; col < 4; col++){
if (tiles[row][col] != null) {
return; // leave this function and don't exit for any non-null tile
}
}
}
System.exit(0);
}