Java无止境While循环

Java无止境While循环,java,if-statement,while-loop,infinite-loop,Java,If Statement,While Loop,Infinite Loop,我有以下代码: 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;

我有以下代码:

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
}
boolean[]usedInts={false,false,false,false,false,false,false,false};

对于(int i=0;i我猜测它是因为GeneraterAnd函数没有返回正确的范围。

我猜测它是因为GeneraterAnd函数没有返回正确的范围。

我看到的唯一可能性是,
GeneraterAnd
方法生成范围为0..7的数字(只有8个数字,而不是9个)或者1..8例如

我看到的唯一可能性是,
generateradom
方法生成范围为0..7的数字(只有8个数字,而不是9个)或者1..8例如

添加生成域方法,这样我们就可以解决您的问题。

添加生成域方法,这样我们就可以解决您的问题。

您需要给我们更多的代码。目前我们不知道
坐标
是什么,正方形
是什么。就目前而言,这个问题非常棘手e需要回答。
generateradom
如何工作?@Jeffrey这才是真正的问题……如果您试图生成坐标的随机排列,那么您可能会发现集合。shuffle方法是更方便的方法(除非您对随机生成器有非常特殊的要求)。
getRandomNumber()
=>
new Random().nextInt()%9
您需要给我们更多的代码。目前我们不知道
坐标是什么,也不知道
平方是什么。就目前而言,这个问题的答案相当模糊。
GeneratorDOM
如何工作?@Jeffrey这才是真正的问题……如果您试图生成坐标的随机排列,那么您可以可能会发现Collections.shuffle方法更方便(除非您对随机生成器有非常特殊的要求)。
getRandomNumber()
=>
new random().nextInt()%9
you and me Tala,我们有这个hahaYep.StackOverflow为开发人员总共节省了数年:))you and me Tala,我们有这个,哈哈。StackOverflow为开发人员总共节省了年:))