我有以下代码:
boolean[] usedInts = {false, false, false, false, false, false, false, false, false};
for(int i = 0; i <= 8; i++) {
JLabel square = squares[i];
// Declare coordinate
Coordinate coordinate = null;
boolean keepGoing = true;
while (keepGoing) {
// Get random number
int rand = generateRandom();
if (usedInts[rand]) {
keepGoing = true;
} else {
// Save that we used it
usedInts[rand] = true;
keepGoing = false;
}
// Initialize coordinate
coordinate = coordinates[rand];
}
// Set square coordinates
square.setLocation(coordinate.getX(), coordinate.getY());
// Set used to true
}
问题是while
循环是无限的,else
部分只运行了 8 次。这里发生了什么?