【发布时间】:2017-04-16 19:22:19
【问题描述】:
我的问题在于以下代码。 Eclipse IDE 没有给我任何错误或警告,但是当我打印出一个简单的System.out.println("Test" + i); 时,我会得到一个正在运行的程序,编号为 2509,或者在重新启动 Eclipse 后当前为 2517。
基本上,我想获取一组对象,比如一组“人”,然后将它们放置在另一个对象数组中的随机点,比如“公共汽车站”。 假设我已经为“busStops”和“people”正确地创建了对象数组
是的,我意识到它违背了制作“人”对象的目的,但这是可以在以后包含的内容。
编辑:空值是人们不能去的模拟区域,例如湖泊。
Edit2:用while循环替换for,用continue关键字替换递减的i。
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 人,这比上面的房间要小。
【问题讨论】:
-
你不应该改变循环内的循环索引。重新考虑您的程序,使其不需要这样做。
-
你的数组长度是多少?即
people.length、busStops.length和busStops[0].length?MAX_PEOPLE_HERE的值是多少? -
people.length是全局变量的 25%(截断整数)。busStops.length是数组中的行数,这里是 4。busStops[0].length在这里是 10。MAX_PEOPLE_HERE取决于它所在的公交车站,因此如果它已满,它会再次返回循环以找到另一个未满的。 -
我会发布您的完整代码(包括您如何分配 MAX_PEOPLE_HERE 以及如何实例化 people 数组)。我很确定上面的代码可以正常工作,但问题是它是如何与你的程序的其余部分交互的。
标签: java eclipse java-8 infinite-loop distribution