[英]Finding repeating numbers within a column in a 2D array
I'm making a digital Bingo board using numbers and I want all the numbers to be unique.我正在使用数字制作数字宾果板,我希望所有数字都是唯一的。 So I'm trying to create an algorithm that will loop through the column, find repeating numbers and then replace them with a random number between two certain numbers.
所以我试图创建一个算法,它将遍历列,找到重复的数字,然后用两个特定数字之间的随机数替换它们。
This is what I have so far:这是我到目前为止所拥有的:
public void bingoBoardGenrerator(int[][] bingoBoard) {
for (int row = 0; row < bingoBoard.length; row++) {
int random = (int) (Math.random() * 15) + 1;
bingoBoard[row][0] = random;
for (int j = 1; j < bingoBoard.length; j++) {
if (bingoBoard[j][0] == random) {
bingoBoard[row][0] = (int) (Math.random() * 15) + 1;
}
}
The output should look like this but without the repeating numbers in the columns: output 应如下所示,但列中没有重复数字:
Free space = 0
B I N G O
5 25 36 53 61
9 19 36 60 62
1 17 0 54 63
6 20 37 57 71
5 19 39 57 69
Any help would be really appreciated!任何帮助将非常感激!
For the "B" column:对于“B”列:
ArrayList
with the Integers 1-15.ArrayList
。Collections.sort()
to randomize the numbers.Collections.sort()
随机化数字。ArrayList
and add them to your Array
ArrayList
中取出前 5 个数字并将它们添加到您的Array
中Repeat the above steps for the I, N, G, O columns each using the appropriate range of numbers.使用适当的数字范围对 I、N、G、O 列重复上述步骤。
Or another approach (for the B) is to:或者另一种方法(对于 B)是:
Repeat the above steps for the other columns.对其他列重复上述步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.