Java 扫描器循环?

标签 java loops java.util.scanner

我正在制作一个迷宫求解器。由于某种原因,每次到达标有“-->”的行时,都会输出“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/

相关文章:

java - 从 SQLite 填充联系人 ListView - 不显示联系人

java - 将启用 Jersey 的 servlet 部署到 Tomcat : Exceptions

带有循环赋值的 Javascript 函数

java - 具有无限预定义值的 ArrayList

java - 数组列表和文件

java - 将 .txt 文件读入二维数组

java - 如何计算球的下一个方向

java - StatelessSession 比 Session 快吗?

excel - 使用相对引用

java - 使用扫描仪 java 时的 If 语句