java - 代码似乎陷入了潜在的循环

标签 java loops arraylist java.util.scanner user-input

我正在用java编写一个CCC问题的答案,但每次我输入一些东西时,它只需要无限的输入。谁能帮我阻止这个?

Your task is to write a program that verifies the validity of a well plan by verifying that the borehole will not intersect itself. A two-dimensional well plan is used to represent a vertical cross-section of the borehole, and this well plan includes some drilling that has occurred starting at (0, −1) and moving to (−1, −5). You will encode in your program the current well plan shown in the figure below:

Input Format:

The input consists of a sequence of drilling command pairs. A drilling command pair begins with one of four direction indicators (d for down, u for up, l for left, and r for right) followed by a positive length. There is an additional drilling command indicated by q (quit) followed by an integer, which indicates the program should stop the execution. You can assume that the input is such that the drill point will not:

  • rise above the ground, nor
  • be more than 200 units below ground, nor
  • be more than 200 units to the left of the original starting point, nor
  • be more than 200 units to the right of the original starting point.

Output Format:

The program should continue to monitor drilling assuming that the well shown in the figure has already been made. As we can see (−1, −5) is the starting position for your program. After each command, the program must output one line with the coordinates of the new position of the drill, and one of the two comments safe, if there has been no intersection with a previous position or DANGER if there has been an intersection with a previous borehole location. After detecting and reporting a self-intersection, your program must stop.

我的代码是:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> holeX = new ArrayList<>();
        ArrayList<Integer> holeY = new ArrayList<>();
        String direction;
        boolean danger = false;
        holeX.add(0);
        holeX.add(0);
        for (int i = 0; i < 4; i++) {
            holeX.add(i);
        }
        holeX.add(3);
        holeX.add(3);
        holeX.add(4);
        holeX.add(5);
        holeX.add(5);
        holeX.add(5);
        holeX.add(6);
        for (int i = -3; i > -8; i--) {
            holeX.add(7);
        }
        for (int i = 6; i > -2; i--) {
            holeX.add(i);
        }
        holeX.add(-1);
        holeX.add(-1);

    holeY.add(-1);
    holeY.add(-2);
    for (int i = 0; i < 4; i++) {
        holeY.add(-3);
    }
    holeY.add(-4);
    holeY.add(-5);
    holeY.add(-5);
    holeY.add(-5);
    holeY.add(-4);
    holeY.add(-3);
    holeY.add(-3);
    for (int i = -3; i > -8; i--) {
        holeY.add(i);
    }
    for (int i = 6; i > -2; i--) {
        holeY.add(-7);
    }
    holeY.add(-6);
    holeY.add(-5);

    do {
        direction = sc.next();
        int steps = sc.nextInt();
        switch (direction) {
            case "d":
                for (int i = holeY.get(holeY.size() - 1); i > holeY.get(holeY.size() - 1) - steps; i--) {
                    holeY.add(i);
                    for (int j = 0; j < holeY.size() - 2; j++) {
                        if (Objects.equals(holeY.get(holeY.size() - 1), holeY.get(j)) && Objects.equals(holeX.get(holeX.size() - 1), holeX.get(j))) {
                            danger = true;
                        }
                    }
                }
            case "u":
                for (int i = holeY.get(holeY.size() - 1); i < holeY.get(holeY.size() - 1) + steps; i++) {
                    holeY.add(i);
                    for (int j = 0; j < holeY.size() - 2; j++) {
                        if (Objects.equals(holeY.get(holeY.size() - 1), holeY.get(j)) && Objects.equals(holeX.get(holeX.size() - 1), holeX.get(j))) {
                            danger = true;
                        }
                    }
                }
                break;
            case "l":
                for (int i = holeX.get(holeX.size() - 1); i > holeX.get(holeX.size() - 1) - steps; i--) {
                    holeX.add(i);
                    for (int j = 0; j < holeX.size() - 2; j++) {
                        if (Objects.equals(holeX.get(holeX.size() - 1), holeX.get(j)) && i == holeY.get(j)) {
                            danger = true;
                        }
                    }
                }
                break;
            case "r":
                for (int i = holeX.get(holeX.size() - 1); i < holeX.get(holeX.size() - 1) + steps; i++) {
                    holeX.add(i);
                    for (int j = 0; j < holeX.size() - 2; j++) {
                        if (Objects.equals(holeX.get(holeX.size() - 1), holeX.get(j)) && i == holeY.get(j)) {
                            danger = true;
                        }
                    }
                }
                break;
            default:
                break;
        }
        if (danger == false && !"q".equals(direction)) {
            System.out.println(holeX.get(holeX.size() - 1) + " " + holeY.get(holeY.size() - 1) + "safe");
            System.out.print(" safe");
        } else {
            System.out.println(holeX.get(holeX.size() - 1) + " " + holeY.get(holeY.size() - 1) + " DANGER");
        }
    } while (!"q".equals(direction) && danger == false);
}

holeXholeY是钻孔区域的坐标。

输入:

l 2
d 2
r 1
q 0

输出:

-3 -5 safe
-3 -7 safe
-2 -7 safe

最佳答案

是的,你有一个无限循环。让我们以case“l”为例。

我使用了l,然后使用了2

for (int i =holeX.get(holeX.size() - 1); i >holeX.get(holeX.size() - 1) - 步骤; i--) { holeX.add(i)...

您向列表中添加一些内容,并且条件会被重新评估,这会导致无限循环。

我不知道这是否是您想要做的,但有帮助的是首先将条件提取到变量中,然后无限循环停止。

关于java - 代码似乎陷入了潜在的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58331482/

相关文章:

java - 在排序文件中使用二进制搜索超快速自动完成(300000 行)

javascript - 使用 settimeout 和 for 循环进行延迟

loops - SBCL 循环中没有 Consing

java - Java中从ArrayList中删除对象

java - ArrayList 类在离开 while 循环后丢失变量

java - 如何在 java 中创建固定大小的通用缓冲区?

Java:有符号长到无符号长字符串

java - webapp/WEB-INF 或 src/main/resources 下的资源文件?

ruby - 在 ruby​​ 中创建两个日期之间的月份范围

Java反序列化和ArrayList奇怪的异常