用于循环关闭/退出应用程序

时间:2015-11-21 15:07:42

标签: java arrays for-loop multidimensional-array exit

当板上的所有图块都= = 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);
                }
            }
        }
    }

1 个答案:

答案 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);
}