Java 扫描器循环?
我正在制作一个迷宫求解器。由于某种原因,每次该行都标有“-->”达到时,输出“输入高度:”。就像那条线(到达时不运行)以某种方式使方法循环。
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
}
任何帮助都比没有帮助好。我已经在这个问题上呆了好几个小时了。谢谢。
I am making a maze solver. For some reason Everytime the line marked with '-->' is reached, "Enter height: " is outputted. It is like that line (which is not run when it is reached) somehow makes the method loop.
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
}
I labelled with '-->' where I believe my problem lays. Any code i put before the marked line will execute, but as soon as that line is reached it loops back to the top of this method. Below is the Map constructor:
public Map(char[][] passMap) {
adjList = new Vertex[map.length*map[0].length];
map = passMap; //stores the passed map
}
ANY HELP is better than no help. I've been at this for hours. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的地图变量可能未初始化。更改:
至:
我还将您的
System.err.println()
调用更改为System.out.println()
。第一个用于错误输出,第二个用于正常控制台输出。Your map variable is probably uninitialized. Change:
To:
I would also change your
System.err.println()
calls toSystem.out.println()
. The first one is for error output, and the second one for normal console output.为什么不学习使用调试器(这很简单)并在那一行设置一个断点。这会很快给你答案。
Why not learn to use a debugger (it's pretty easy) and just set a breakpoint on that line. That would give you your answer real quick.