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

时间:2017-04-16 19:22:20

标签: java eclipse java-8 infinite-loop distribution

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

基本上,我想获取一个对象数组,比如一个“人物”数组,并将它们放在另一个对象数组中的随机点中,比如说“公共汽车站”。 假设我已经为“busStops”和“人”正确制作了对象数组

是的,我意识到它打破了制作“人物”对象的目的,但这可以在以后加入。

编辑:空值是人们无法去的模拟区域,如湖泊。

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);

}

从相应的BusStop类:

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人,小于上面的房间。

2 个答案:

答案 0 :(得分:3)

好的,你的问题是你为MAX_PEOPLE_HERE使用静态变量,但是你试图以非静态的方式使用它。因此,有时你在任何公共汽车站上调用setMaxNumberOfPeople,你都会为所有公共汽车站设置它。

这意味着MAX_PEOPLE_HERE将最终为557 + 1961 = 2518.

我猜测numberOfPeopleHere也是静态的,因此你只能有2518人到巴士站。如果你试图做更多的事情,那么你将会看到无限循环。

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

答案 1 :(得分:2)

使用continue代替i--跳过当前迭代。正如@Hovercraft Full Of Eels所述,由于循环内的索引修改,你已经有了无限循环