我创建了一个方法,它使用 1 到 75 的整数来流行一个 5 x 5 数组,为宾果游戏卡奠定了基础。
public static void bingoCard(){
int [][]card=new int [5][5];
ArrayList<Integer> alreadyUsed = new ArrayList<Integer>();
boolean valid = false;
int tmp = 0;
for(int row=0; row < card.length; row++){
while(!valid){
tmp = (int)(Math.random()*15)+1;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][0]= tmp;
valid = false;
}
for(int row=0;row<card.length;row++){
while(!valid){
tmp = (int)(Math.random()*15)+16;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][1]= tmp;
valid = false;
}
for(int row=0;row<card.length;row++){
while(!valid){
tmp = (int)(Math.random()*15)+31;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][2]= tmp;
valid = false;
}
card[2][2]=0;
for(int row=0;row<card.length;row++){
while(!valid){
tmp = (int)(Math.random()*15)+46;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][3]= tmp;
valid = false;
}
for(int row=0;row<card.length;row++){
while(!valid){
tmp = (int)(Math.random()*15)+61;
if(!alreadyUsed.contains(tmp)){
valid = true;
alreadyUsed.add(tmp);
}
}
card[row][4]= tmp;
valid = false;
}
//create array to make title.
String title []={"B","I","N","G","O"};
for(int i=0;i<title.length;i++){
System.out.print(title[i]+ "\t");
}
System.out.println();
for(int row=0;row<card.length;row++){
for(int col=0;col<card[row].length;col++){
System.out.print(card[row][col]+ "\t");
}
System.out.println();
}
样本输出:
B I N G O
14 23 31 49 66
13 29 45 52 65
15 20 0 60 64
5 21 33 54 63
6 16 40 57 71
我对压缩代码很感兴趣,因为到目前为止,它重复了五行。我能得到一些建议吗?谢谢你。