For 循环到 close/exit 应用程序

For loop to close/exit application

当棋盘上的所有图块都 == 为空时,此代码应该关闭应用程序,但目前当循环运行时,它会在发现只有 1 个图块为空时立即退出应用程序。我该如何解决?

谢谢!

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

因为你的逻辑不对。它应该像下面这样。

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