Java 扫描器循环?

发布于 2024-11-17 21:08:54 字数 1103 浏览 4 评论 0原文

我正在制作一个迷宫求解器。由于某种原因,每次该行都标有“-->”达到时,输出“输入高度:”。就像那条线(到达时不运行)以某种方式使方法循环。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度℉ 2024-11-24 21:08:54

您的地图变量可能未初始化。更改:

adjList = new Vertex[map.length*map[0].length];

至:

adjList = new Vertex[passMap.length*passMap[0].length];

我还将您的 System.err.println() 调用更改为 System.out.println()。第一个用于错误输出,第二个用于正常控制台输出。

Your map variable is probably uninitialized. Change:

adjList = new Vertex[map.length*map[0].length];

To:

adjList = new Vertex[passMap.length*passMap[0].length];

I would also change your System.err.println() calls to System.out.println(). The first one is for error output, and the second one for normal console output.

薄荷梦 2024-11-24 21:08:54

为什么不学习使用调试器(这很简单)并在那一行设置一个断点。这会很快给你答案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文