import java.util.ArrayList;




int randomNum, high = 0, low = -14, row = -1;
Boolean goAgain;




ArrayList<Integer> numberList = new ArrayList<>();

do {

        high = high + 15;
        low = low + 15;
        row = row + 1;
        goAgain = true;

        for (int colum = 0; colum < 5; colum++) {

            do {

                randomNum = (int) (Math.random() * (high - low) + low);

                containsNumber = numberList.contains(randomNum);

                if (containsNumber == false) {

                    numberList.add(randomNum);

                    bingoBoard[row][colum] = randomNum;

                    goAgain = false;

                }

            } while (goAgain = true);
        }
    } while (row != 4);


我正在尝试创建一个宾果卡。当我运行上面的代码时,我陷入了最内部的do-while语句的无限循环中。我知道它可能很容易解决,但我严重看不到问题...

最佳答案

您在以下行中将true分配给goAgain

} while (goAgain = true);


运算符=是分配运算符。因此内部的do-while循环将永远不会结束。

您可以使用goAgain比较运算符将true==进行比较,但是goAgain已经是boolean,因此您可以只使用值本身。

} while (goAgain);

10-05 18:54