这是一个学校项目。这是一个宾果游戏,我正在处理的部分的说明是使用2D阵列填充宾果卡。第一个(B)列必须具有1到15的随机数,没有重复的数字。第二个(I)列必须具有16到30的randoms,从31到45的第三个(N),依此类推。我设置了填充和显示卡片,但我得到了重复。我无法弄清楚如何针对特定列中的每一行检查随机数以查看它是否已被使用,并根据需要更改该数字。我理解我必须在逻辑上做什么,但我不知道如何编码。在B部分下面是我试图开始的地方。我尝试过很多东西,但无济于事。这是我的代码,请帮助:
import java.util.Random;
public class BingoCard
{
Random randNum = new Random();
int[][] numberCard = new int[5][5];
boolean[][] shadowCard = new boolean[5][5];
/** Constructor */
// set up 2D arrays for bingo cards, and card comparison
public BingoCard()
{
// create number card
// fill B column with random numbers from 1 to 15
for(int i = 0; i < numberCard.length; i++)
{
this.numberCard[i][0] = randInt(1, 15);
// check for double numbers and recall a random
while(this.numberCard[i][0] != numberCard[i][0])
{
i++;
}
if(this.numberCard[i][0] == numberCard[i][0])
{
numberCard[i][0] = randInt(1, 15);
}
}
// fill I column with random numbers from 16 to 30
for(int j = 0; j < numberCard.length; j++)
{
numberCard[j][1] = randInt(16, 30);
// check for double numbers and recall a random
}
// fill N column with random numbers from 31 to 45
for(int k = 0; k < numberCard.length; k++)
{
numberCard[k][2] = randInt(31, 45);
numberCard[2][2] = 0; // free space
// check for double numbers and recall a random
}
// fill G column with random numbers from 46 to 60
for(int m = 0; m < numberCard.length; m++)
{
numberCard[m][3] = randInt(46, 60);
// check for double numbers and recall a random
}
// fill O column with random numbers from 61 to 75
for(int n = 0; n < numberCard.length; n++)
{
numberCard[n][4] = randInt(61, 75);
// check for double numbers and recall a random
}
// create shadow card to compare to main card
for(int i = 0; i < shadowCard.length; i++)
{
for(int j = 0; j < 5; j++)
{
shadowCard[i][j] = false;
shadowCard[2][2] = true;
}
}
}
/** Methods */
// method to generate a random number in intervals
private int randInt(int min, int max)
{
int random;
random = randNum.nextInt(max - min) + min;
return random;
}
// method to print the numberCard
public String printNumCard()
{
String string = "";
for(int row = 0; row < numberCard.length; row++)
{
for(int col = 0; col < numberCard[row].length; col++)
{
System.out.print(numberCard[row][col] + " ");
}
System.out.println();
}
return string;
}
/** main method for testing */
public static void main(String[] args)
{
BingoCard bc = new BingoCard();
bc.printNumCard();
}
}