【发布时间】:2014-11-20 13:56:55
【问题描述】:
这是一个学校项目。这是一个宾果游戏,我正在处理的部分的说明是使用 2D 数组填充宾果卡。第一 (B) 列必须有从 1 到 15 的随机数,并且没有重复的数字。第二 (I) 列必须有 16 到 30 的随机数,第三 (N) 列必须有 31 - 45 的随机数,依此类推。我已将其设置为填充和显示卡片,但我得到了重复。我不知道如何针对特定列中的每一行检查随机数以查看它是否已被使用,并根据需要更改该数字。我明白我必须在逻辑上做什么,但我不知道如何编码。在 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();
}
}
【问题讨论】: