二维数组未在 grid/not 完整数组中打印出来?

2D array not printing out in grid/not full array?

我遇到的问题是二维数组没有打印出数组中的所有内容,当我 运行 进化方法时,它打印出的二维数组与第一次。想知道在进化方法 运行s 时不打印出整个数组和不以相同方式打印数组有什么问题吗?阵列的每个打印件都需要在 25x75 网格中

期望的初始结果:

实际结果:

进化方法结果: 项目 4:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Project4 {

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in); // Created a scanner
        System.out.println("Enter the file name you would like to use");
        File file = new File(input.nextLine()); // Takes file name to find file
        Scanner inputFromFile = new Scanner(file);
        FileInputStream fileInput = new FileInputStream(file); // reads file
        int r;
        int y = 0;
        int i = 0;
        while ((r = fileInput.read()) != -1) { // goes through each character in
                                                // file, char by char
            char c = (char) r;
            GameOfLife.grid[i][y] = c;
            y++;
            if (y == 75) {
                y = 0;
                i++;
                if (i == 25) {
                    break;
                }
            }
        }
        // Prints the initial environment
        System.out.println("Initial set: ");
        for (int j = 0; j < GameOfLife.grid.length; j++) {
            System.out.print(GameOfLife.grid[j]);
        }
        boolean operate = true;
        while (operate = true) {
            System.out.println("Do you want to see the next generation? Y/N?");
            String q = input.nextLine();
            if (q.equalsIgnoreCase("y")) {
                GameOfLife.evolve();
                for (int j = 0; j < GameOfLife.grid.length; j++) {
                    System.out.print(GameOfLife.grid[j]);
                    operate = true;
                }
            } else {
                operate = false;
                System.exit(0);
            }
        }
    }
}

生命游戏:

import java.util.Arrays;

public class GameOfLife {

    static final int m = 25; // number of rows
    static final int n = 75; // number of columns
    static char[][] grid = new char[m][n]; // Creates an empty (no dots or
                                            // X's)grid of rows and columns.

    static int count_neighbors(int i, int j) {
        int nn = 0; // number of neighbors of cell(i,j)

        if (i > 0 && j > 0 && grid[i - 1][j - 1] == 'X') {
            nn++;
        }
        ;
        if (i > 0 && grid[i - 1][j] == 'X') {
            nn++;
        }
        ;
        if (i > 0 && j < 72 && grid[i - 1][j + 1] == 'X') {
            nn++;
        }
        ;
        if (j > 0 && grid[i][j - 1] == 'X') {
            nn++;
        }
        ;
        if (j < 72 && grid[i][j + 1] == 'X') {
            nn++;
        }
        ;
        if (j > 0 && i < 22 && grid[i + 1][j - 1] == 'X') {
            nn++;
        }
        ;
        if (i < 22 && grid[i + 1][j] == 'X') {
            nn++;
        }
        ;
        if (i < 22 && j < 72 && grid[i + 1][j + 1] == 'X') {
            nn++;
        }

        return nn;
    }

    static void evolve() {
        for (int i = 0; i < 23; i++) {
            for (int j = 0; j < 73; j++) {
                int s = count_neighbors(i, j);
                if (s < 2) {
                    grid[i][j] = '.';
                }
                if (s == 2 || s == 3) {
                    grid[i][j] = 'X';
                }
                if (s > 3) {
                    grid[i][j] = '.';
                }
            }

        }

    }

}

我认为你应该像这样在两个地方使用 println 这个:

System.out.println(GameOfLife.grid[j]);

System.out.print() 不会自动刷新,只有在写入换行符时才会刷新。这意味着与其写出所有内容,不如缓冲 IO 以提高效率。

在每个 for 循环之后添加一个 System.out.println() 以在下一行开始打印网格。

// Print out grid.length characters
for (int j = 0; j < GameOfLife.grid.length; j++) {
    System.out.print(GameOfLife.grid[j]);
}
// Print a new line
System.out.println();

已更新

我在上面没有注意到的问题是,您在一行中打印一整行,并且在一个(可见)循环中打印整个网格。因此,下面的 System.out.println() 将在打印整个网格后打印一个空行。请参阅我的解决方案,它提取游戏的打印,对文件 reader 使用 try-catch,并删除底部不必要的 operate 变量,因为您只是退出了。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Project4 {

    private static void printGame() {
        for (char[] row : GameOfLife.grid) {
            for (char c : row) {
                System.out.print(c);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in); // Created a scanner
        System.out.println("Enter the file name you would like to use");
        File file = new File(input.nextLine()); // Takes file name to find file
        BufferedReader br = null;
        String line;
        int i = 0;

        try {
            br = new BufferedReader(new FileReader(file));
            while ((line = br.readLine()) != null) {
                for (int col = 0; col < line.length(); col++) {
                    GameOfLife.grid[i][col] = line.charAt(col);
                }
                i++;
                if (i == 25) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                br.close();
            }
        }

        // Prints the initial environment
        System.out.println("Initial set: ");
        printGame();

        while (true) {
            System.out.println("Do you want to see the next generation? Y/N?");
            String q = input.nextLine();
            if (q.equalsIgnoreCase("y")) {
                GameOfLife.evolve();
                printGame();
            } else {
                System.exit(0);
            }
        }
    }
}