我正在制作一个迷宫求解器。由于某种原因,每次到达标有“-->”的行时,都会输出“Enter height:”。就像那条线(到达时不会运行)以某种方式使方法循环。
private void makeMap() {
Map map; //used to convert the char array into a graph
int height; //the height of the map by user input
char[][] array; //used to store the char map
System.err.println("Enter height: ");
height = scanner.nextInt(); //gets and stores the height from the user
array = new char[height][]; //initializes the map with input height
for(int i=0; i<height; i++) { //adds row by row to the map array
System.err.print("Enter next line of map: ");
array[i] = scanner.next().toCharArray();
}
--> map = new Map(array); //initializes the map by passing the char array
graph = map.makeGraph(); //creates a graph from the char array
}
我在我认为问题所在的地方标上了“-->”。我在标记行之前放置的任何代码都将执行,但是一旦到达该行,它就会循环回到该方法的顶部。下面是 Map 构造函数:
public Map(char[][] passMap) {
adjList = new Vertex[map.length*map[0].length];
map = passMap; //stores the passed map
}
任何帮助都比没有帮助好。我已经在这个问题上呆了好几个小时了。谢谢。
最佳答案
您的 map 变量可能未初始化。变化:
adjList = new Vertex[map.length*map[0].length];
致:
adjList = new Vertex[passMap.length*passMap[0].length];
我还将您的 System.err.println()
调用更改为 System.out.println()
。第一个用于错误输出,第二个用于正常控制台输出。
关于Java 扫描器循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6550946/