[英]How do i stop repeating numbers?
我必须打印一个1-15、16-30等的宾果游戏板。 我已经印制了黑板,但是我不断重复输入数字。 在我的显示方法中,我似乎无法查明为什么重复数字的if语句不起作用。 我也是这个网站的新手,但是如果有一些不清楚的地方,我会回应。
public class BingoArraysAssignment {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[][] board1 = new int[5][5];
int[][] board2 = new int[5][5];
int[][] board3 = new int[5][5];
display(board1);
//display2(board3);
//display(board2);
}
public static void display(int[][] board1) {
//int[][] board1 = new int[5][5];
int z = 1, v = 15, y, counter = 0;
for (int i = 0; i <= 4; i++) {
for (int j = 0; j <= 4; j++) {
//counter++;
y = (int) (Math.random() * v + z);
if (board1[i][j] == y) {
j--;
set(board1[i][j], 0);
}
if (board1[i][j] == 0) {
board1[i][j] = y;
System.out.print("|" + board1[i][j]);
//break;
}
}
z = z + 15;
v = v + 15;
System.out.println(" ");
}
}
}
随机数可以重复。 您必须创建一个由25个数字组成的随机序列并使用它:
List<Integer> numbers = new List();
list.add(...);
Collections.shuffle(numbers);
Iterrator<Integer> numberItr = list.iterrator();
.....
y = numberItr.next();
不要使用Math.Random()
, Math.Random()
要做Bingo在现实生活中所做的事情。 创建一个包含所有所需数字的列表,然后从列表中随机抽取一个数字。
下次从中提取时,您将不会获得相同的值。
创建一个包含数字1到75的列表。从1-15中随机选择5,从16-30中随机选择5,依此类推...然后从列表中删除您选择的数字。 这样,您将不会再有任何数字。
这是一个例子
//array need numbers from 1 to 15(Can be populated with a loop)
int[] colm = { 1, 2, 3, 4, 5, 14, 15}; //Enter 1 to 15
int[] colm2 = {16,17,18,19,20,21,22,30};//...16 to 30
int[][] board = new int[5][5];
//...
List l = new ArrayList();
for(int i: colm)
l.add(i);
Collections.shuffle(l);
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if(i=0) board[i][j] = colm1[j]
if(i=1) board[i][j] = colm2[j]
//...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.