Java 8:没有错误 - 为什么这个 for 循环 运行 永远不显示任何内容?

Java 8: No errors - Why is this for loop running forever and not showing anything?

我的问题与以下代码有关。 Eclipse IDE 没有给我任何错误或警告,但是当我打印出一个简单的 System.out.println("Test" + i); 时,我会得到一个 运行 编号为 2509 的程序,或者在重新启动 Eclipse 后当前为 2517。

本质上,我想获取一个对象数组,比如一个 "persons," 数组,并将它们放置在另一个对象数组中的 随机 点,比如 "bus stops." 假设我已经为"busStops and "人正确制作了对象数组

是的,我意识到它违背了制作 "person" 对象的目的,但这是可以稍后包含的内容。

编辑:空值是人们不能去的模拟区域,例如湖泊。

Edit2:将 for 替换为 while 循环,将递减的 i 替换为 continue 关键字。

Edit3:添加了更多方法来详细说明我的代码的缺陷。话又说回来,也许大部分都很好,但我不了解有关循环的重要内容。

private static void distributePeople() {

    boolean temp = true;
    int i = 0;

    while (temp) {
        // Select random points in array
        int a = rand.nextInt(busStops.length);
        int b = rand.nextInt(busStops[0].length);

        // At random busStop, check if available and check if not full.
        // If it is not full, place a person there.
        if (busStops[a][b] == null) {
            // if null, reset this run
            continue;
        } else {
            if (busStops[a][b].isMaxPeople() == false) {
                busStops[a][b].setNumberOfPeople(1);
                i++;
                System.out.println("Test: " + i);
            } else {
                // if true, reset this run
                continue;
            }
        }
        if (i == people.length) {
            temp = false;
        }
    }
}

private static void setMaxPeopleAtBusStop() {
    busStops[0][0].setMaxNumberOfPeople(1977 + 2);
    busStops[1][0].setMaxNumberOfPeople(2 + 1643);
    busStops[2][0].setMaxNumberOfPeople(1643 + 1201);
    busStops[3][0].setMaxNumberOfPeople(1201 + 1267);
    busStops[0][1].setMaxNumberOfPeople(366 + 0);
    busStops[2][1].setMaxNumberOfPeople(0 + 797);
    busStops[3][1].setMaxNumberOfPeople(797 + 34);
    busStops[0][2].setMaxNumberOfPeople(1740 + 0);
    busStops[2][2].setMaxNumberOfPeople(0 + 1444);
    busStops[3][2].setMaxNumberOfPeople(1444 + 1963);
    busStops[0][3].setMaxNumberOfPeople(839 + 1131);
    busStops[1][3].setMaxNumberOfPeople(1131 + 1092);
    busStops[2][3].setMaxNumberOfPeople(1092 + 912);
    busStops[3][3].setMaxNumberOfPeople(912 + 1965);
    busStops[0][4].setMaxNumberOfPeople(1552 + 1297);
    busStops[1][4].setMaxNumberOfPeople(1297 + 1345);
    busStops[2][4].setMaxNumberOfPeople(1345 + 614);
    busStops[3][4].setMaxNumberOfPeople(614 + 1108);
    busStops[0][5].setMaxNumberOfPeople(1490 + 228);
    busStops[1][5].setMaxNumberOfPeople(228 + 187);
    busStops[2][5].setMaxNumberOfPeople(187 + 906);
    busStops[3][5].setMaxNumberOfPeople(906 + 36);
    busStops[0][6].setMaxNumberOfPeople(634 + 1293);
    busStops[1][6].setMaxNumberOfPeople(1293 + 0);
    busStops[3][6].setMaxNumberOfPeople(0 + 1929);
    busStops[0][7].setMaxNumberOfPeople(759 + 388);
    busStops[1][7].setMaxNumberOfPeople(388 + 0);
    busStops[3][7].setMaxNumberOfPeople(0 + 1149);
    busStops[0][8].setMaxNumberOfPeople(1809 + 1880);
    busStops[1][8].setMaxNumberOfPeople(1880 + 1979);
    busStops[2][8].setMaxNumberOfPeople(1979 + 954);
    busStops[3][8].setMaxNumberOfPeople(954 + 1332);
    busStops[0][9].setMaxNumberOfPeople(1890 + 408);
    busStops[1][9].setMaxNumberOfPeople(408 + 1771);
    busStops[2][9].setMaxNumberOfPeople(1771 + 587);
    busStops[3][9].setMaxNumberOfPeople(557 + 1961);

}

从适当的公交车站class:

static int MAX_PEOPLE_HERE;

public int setNumberOfPeople(int a) {
    return numberOfPeopleHere += a;
}

protected boolean isMaxPeople() {
    if (numberOfPeopleHere >= MAX_PEOPLE_HERE) {
        return true;
    } else {
        return false;
    }
}

public void setMaxNumberOfPeople(int a) {
    MAX_PEOPLE_HERE = a;
}

注意:我最多能容纳13000人,比上面的房间小

使用 continue 而不是 i-- 来跳过当前迭代。正如@Hovercraft Full Of Eels 所说,由于循环内的索引修改,你有无限循环

好的,所以您的问题是您正在为 MAX_PEOPLE_HERE 使用静态变量,但您正试图以非静态方式使用它。因此,每当您在任何公交车站调用 setMaxNumberOfPeople 时,您都将其设置为 all 个公交车站。

这意味着 MAX_PEOPLE_HERE 最终将是 557 + 1961 = 2518。

我猜 numberOfPeopleHere 也是静态的,因此您只能让 2518 人到达公交车站。如果您尝试做更多的事情,那么您最终会看到一个无限循环。

将 MAX_PEOPLE_HERE(将其重命名为 maxPeopleHere)和 numberOfPeopleHere 都更改为本地实例变量,我怀疑一切都会开始工作。