You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java读取文件生成康威生命游戏棋盘矩阵输出偏移异常问题

康威生命游戏棋盘生成方法偏移问题修复

问题场景

  • 复刻康威生命游戏时,实现的文件读取、棋盘生成方法运行不符合预期,输出矩阵逐次偏移,多次调整代码未解决。
  • 方法预期功能:读取指定输入文件,生成按单个单元格存储的二维矩阵棋盘。

原有问题代码

public static String[][] originalBoardCreation() {
        Scanner sc= MyUtils.readFile(inpFileName);
        
        width = sc.nextInt();
        height = sc.nextInt();  
        sc.nextLine();
        
        String[][] board = new String[width][height];
        String[] line = new String[board.length];
        
        while (sc.hasNext()) {
            for (int i = 0; i < board.length; i++) {
                line[i] = sc.nextLine().trim();
                
                for (int j = 0; j < line.length; j++) {
                    board[i][j] = line[j];
                }
            }
        } 
        
        return board;
    }

异常表现

调用代码:

System.out.println(Arrays.deepToString(originalBoardCreation()));

实际输出:

[[.xxxxxxxx., null, null, null, null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., null, null, null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., null, null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., ....xxx..., null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., ....xxx..., .........., null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., ....xxx..., .........., ..........]]

预期输出:二维矩阵每个元素为单个字符(x或.),格式参考[[., x, x, x, x, x, x, x, x, .], ... 其余元素按单个字符拆分存储]

代码逻辑错误点

  • 二维数组维度定义错误:读取的width为棋盘列数(每行字符数)、height为棋盘行数,原代码直接用new String[width][height]初始化数组,颠倒行列对应关系。
  • 中间数组逻辑完全错误:原代码定义的line数组长度等于棋盘行数,每次仅给line[i]赋值为当前读取到的整行字符串,其余位置保留默认null值;内层循环遍历line数组给board[i][j]赋值时,只会把当前及之前读取到的整行字符串依次放入当前行的前i+1个位置,后续位置全为null,直接导致逐行偏移的异常表现。
  • 缺失字符拆分逻辑:原代码直接将整行字符串作为单个元素存入棋盘,未将字符串拆分为单个字符,完全不符合单元格单字符存储的预期。
  • 存在冗余循环:已经通过for循环按固定行数读取内容,外层套while (sc.hasNext())属于多余逻辑,无实际作用还可能引发重复读取问题。

修正后实现代码

public static String[][] originalBoardCreation() {
    Scanner sc = MyUtils.readFile(inpFileName);
    // 读取棋盘尺寸
    int width = sc.nextInt();
    int height = sc.nextInt();
    // 消费读取整数后残留的换行符
    sc.nextLine();

    // 正确初始化二维数组:行=height,列=width
    String[][] board = new String[height][width];

    for (int i = 0; i < height; i++) {
        String currentLine = sc.nextLine().trim();
        // 逐字符拆分存入对应单元格
        for (int j = 0; j < width; j++) {
            board[i][j] = String.valueOf(currentLine.charAt(j));
        }
    }
    sc.close();
    return board;
}

内容的提问来源于stack exchange,提问作者baddev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 16:57:17