1

次のコードがあります。

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 回しか実行されないことです。ここで何が起こっているのですか?

4

3 に答える 3

3

私が見る唯一の可能性は、たとえば、generateRandomメソッドが範囲0..7(9ではなく8つの数字のみ)または1..8の数字を生成することです

于 2013-07-01T19:37:57.233 に答える
2

generateRandom 関数が正しい範囲を返していないためだと思います。

于 2013-07-01T19:37:56.317 に答える