我无法将字符放入文件中的二维数组中。当我打印出阵列时,我得到null
,我无法弄清楚原因。
程序读入一个文件,格式化第一行包含2个数字,分别对应高度和宽度,首先检查命令行参数是否有效。如果是,则读取文件,根据这两个数字创建一个二维数组,然后将文件的其余部分存储到数组中。文件的其余部分是一个迷宫般的字符,如下所示:
2 3
+-+-+-+
|S| |
+ + + +
| |E|
+-+-+-+
因此,2d阵列模仿迷宫结构。方法readMazeFile
是我在努力的地方。
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)) {
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 void readMazeFile(String mazefile, int arrayHeight, int arrayWidth) throws FileNotFoundException {
Scanner scanner = new Scanner(new File(mazefile));
height = scanner.nextInt();
width = scanner.nextInt();
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);
}
}
}
}
// Solve the maze.
static boolean solveMaze() {
return true;
}
}
这是输出:
+-+-+-+
|S| |
+ + + +
| |E|
+-+-+-+
null
Solved!
感谢您的帮助,我为任何业余缺陷道歉,我正在努力学习Java。我想把它保留在这个结构中。在此之后,我必须使用2D阵列通过递归动画解决迷宫的解决方案。
答案 0 :(得分:0)
从readMazeFile返回数组并将其分配到相同的数组中:
mazeArrays = readMazeFile(mazefile, arrayHeight, arrayWidth);
chage返回类型为char [] []
static char[][] readMazeFile(String mazefile, int arrayHeight, int arrayWidth) throws FileNotFoundException
您将获得null,因为您将mazeArrays作为类变量以及局部变量,因此值已更新为局部变量而非类变量。所以你需要返回mazeArrays数组,并在main方法中使用mazeArrays进行分配。
答案 1 :(得分:0)
我认为你的问题出在这一行:
char[][] mazeArrays = new char[arrayHeight][arrayWidth];
基本上,当您稍后修改mazeArrays
变量时,您将覆盖此局部变量,而不是方法之外的静态变量。删除char[][]
可以解决您的问题。
(像这样)
mazeArrays = new char[arrayHeight][arrayWidth];