gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:06:46 24 4
gpt4 key购买 nike

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

本质上,我想获取一个对象数组,例如“人”数组,并将它们放置在另一个对象数组中的随机位置,例如“公交车站”。 假设我已经正确地为“busStops”和“people”创建了对象数组

是的,我意识到它到目前为止还达不到创建“person”对象的目的,但这是可以稍后包含的内容。

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

编辑2:将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 人,这比上面的可用房间要小。

最佳答案

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

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

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

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

关于Java 8 : No errors - Why is this for loop running forever and not showing anything?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43441151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com