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语句中的无限循环。我知道这可能是一个简单的解决办法,但我真的看不出问题......
答案 0 :(得分:4)
您要在此行中为true
分配goAgain
:
} while (goAgain = true);
运算符=
是赋值运算符。所以内部的do-while循环永远不会结束。
您可以将goAgain
与true
与==
比较运算符进行比较,但goAgain
已经是boolean
,因此您只需使用值本身
} while (goAgain);