有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将输入迷宫文件中的字符存储到2d数组(Java)

我在将正确的值放入2d数组时遇到问题

我的代码应该是用来解迷宫的。它从输入读取迷宫结构。txt文件,将结构排列为二维数组,然后将迷宫绘制到绘图面板中,然后将为解决方案设置动画。我被困在将结构放入阵列中

我的代码从如下输入文件中读取:

2 3
+-+-+-+
|S|   |
+ + + +
|   |E|
+-+-+-+

前两个数字是迷宫的高度和宽度。这个迷宫有两行高,三列宽

这是到目前为止我的代码

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class MazeSolver {

   // The name of the file describing the maze
   static String mazefile;
   static int width;
   static int height;
   static int arrayWidth;
   static int arrayHeight;
   static char[][] mazeArrays;   
   public static void main(String[] args) throws FileNotFoundException {
      if (handleArguments(args)) {

         mazeArrays = readMazeFile(mazefile, arrayHeight, arrayWidth);
         System.out.println(Arrays.deepToString(mazeArrays));
         DrawMaze.draw(mazeArrays, height, width);

         if (solveMaze())
            System.out.println("Solved!");
         else
            System.out.println("Maze has no solution.");
      }
      else {
      System.out.println("The arguments are invalid.");
      }
   }

   // Handle the input arguments
   static boolean handleArguments(String[] args) {
      if (args.length > 4 || args.length < 1) {
         System.out.println("There are too many or too few command line arguments");
         return false;
      }
      if (args.length == 1) {
         mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         return true;
      }
      if (args.length == 2) {
         mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         if (cellsize < 10) {
            return false;
         }
         return true;
      }
      if (args.length == 3) {
         mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         int borderwidth = Integer.parseInt(args[2]);
         if (borderwidth < 5) {
            return false;
         }
         return true;
      }
      if (args.length == 4) {
         mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         int borderwidth = Integer.parseInt(args[2]);
         int sleeptime = Integer.parseInt(args[3]);
         if (sleeptime < 0 || sleeptime > 10000) {
            return false;
         }
         return true;
      }   
      return false;
   }

   // Read the file describing the maze.
   static char[][] readMazeFile(String mazefile, int arrayHeight, int arrayWidth) throws FileNotFoundException {

      Scanner scanner = new Scanner(new File(mazefile));
      height = scanner.nextInt();
      System.out.println(height);
      width = scanner.nextInt();
      System.out.println(width);
      arrayHeight = 2 * height + 1;
      arrayWidth = 2 * width + 1;
      char[][] mazeArrays = new char[arrayHeight][arrayWidth];
      while (scanner.hasNextLine()) {
         String line = scanner.nextLine();
         System.out.println(line);
         for (int row = 0; row < arrayHeight; row++) {
           for (int col = 0; col < line.length(); col++) {
               mazeArrays[row][col] = line.charAt(col);




           }
         }



      }return mazeArrays;

   }

   // Solve the maze.      
   static boolean solveMaze() {
      return true;
   }
}

问题在于方法readMazeFile

以下是输出:

2
3

+-+-+-+
|S|   |
+ + + +
|   |E|
+-+-+-+
[[+, -, +, -, +, -, +], [+, -, +, -, +, -, +], [+, -, +, -, +, -, +], [+, -, +, -, +, -, +], [+, -, +, -, +, -, +]]
Solved!

看起来我的问题是扫描仪永远不会移动到下一行,但我相信我写的是正确的。二维数组正确地存储了第一行,但没有移动到“|S | |”及其后

我还预计在数组中存储空白会出现问题

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    您在输出中看到的是输入的最后一行,而不是第一行。这是因为您正在迷宫的每一行中存储每一行:

    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        System.out.println(line);
        for (int row = 0; row < arrayHeight; row++) {
            for (int col = 0; col < line.length(); col++) {
                mazeArrays[row][col] = line.charAt(col);
            }
        }
    }
    

    我怀疑你打算:

    for (int row = 0; row < arrayHeight; row++) {
        String line = scanner.nextLine();
        System.out.println(line);
        for (int col = 0; col < arrayWidth; col++) {
            mazeArrays[row][col] = line.charAt(col);
        }
    }