为什么我的方法扫描文件并打印出结果比上一次打印1空间?

发布于 2025-02-13 11:20:58 字数 1902 浏览 2 评论 0原文

我目前正在编写一个很长的代码来重现康威的生活游戏,并且刚刚意识到,当我运行代码时,我通过文件扫描并将其打印出来的方法是因为矩阵无法正常工作。我已经尝试解决它,但无济于事。

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(oinartionBoardCreation()))调用Main

[[.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,。],...等等,但是我不知道为什么会发生这种情况或我的错误逻辑,谢谢!

I'm currently writing a quite lengthy piece of code to recreate Conway's Game of Life and have just realized that when I run my code, the method that I made to scan through a file and print itself out as matrix doesn't work as intended. I've tried messing around with it, but to no avail.

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

When I call it to main with System.out.println(Arrays.deepToString(originalBoardCreation())); , I get

[[.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..., .........., ..........]]

I'm trying to get something like [[., x, x, x, x, x, x, x, x, .], ... etc , but I have no clue as to why this is happening, so it would be great if I could get a brief explanation or error in my logic, thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┾廆蒐ゝ 2025-02-20 11:20:58

我相信问题是由于您从文件中读取一行文本并期望将其神奇地 split splin到一个字符串数组中引起的。

public static String[][] originalBoardCreation()
{
  Scanner sc= MyUtils.readFile(inpFileName);

  width = sc.nextInt();
  height = sc.nextInt();  
  sc.nextLine();

  String[][] board = new String[width][height];

  while ( sc.hasNext() )
  {
    for ( int i = 0; i < board.length; i++ )
    {
      String line = sc.nextLine().trim();  // read the text line in
      char tiles[] = line.toCharArray();    // now split the line into an array

      for ( int j = 0; j < tiles.length; j++ )
      {
        board[i][j] = String.valueOf(tiles[j]);  // Now assign the elements to the current row on your board
      }
    }
  } 
  return board;
}

我没有测试过,但是类似的事情应该起作用。

I believe the problem is being caused by the fact that your reading a line of text from a file in and expecting it to be magically split for you into a string array.

public static String[][] originalBoardCreation()
{
  Scanner sc= MyUtils.readFile(inpFileName);

  width = sc.nextInt();
  height = sc.nextInt();  
  sc.nextLine();

  String[][] board = new String[width][height];

  while ( sc.hasNext() )
  {
    for ( int i = 0; i < board.length; i++ )
    {
      String line = sc.nextLine().trim();  // read the text line in
      char tiles[] = line.toCharArray();    // now split the line into an array

      for ( int j = 0; j < tiles.length; j++ )
      {
        board[i][j] = String.valueOf(tiles[j]);  // Now assign the elements to the current row on your board
      }
    }
  } 
  return board;
}

I haven't tested this, but something like this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文