I have to print a bingo board which goes from 1-15,16-30 and so on. I have the board printed out but I keep getting repeating numbers. In my display method I can't seem to pinpoint why the if statement for repeating numbers doesn't work. I'm also new to this website but if there's some unclear points I will respond to them.
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(" ");
}
}
}
random number can repeat. You must create a random sequence of 25 numbers and use it:
List<Integer> numbers = new List();
list.add(...);
Collections.shuffle(numbers);
Iterrator<Integer> numberItr = list.iterrator();
.....
y = numberItr.next();
Don't use Math.Random()
, do what Bingo does in real life. Create a list with all the numbers needed, and extract one number randomly from the list.
Next time you extract from it, you won't get the same value.
Create a list containing the numbers 1 to 75. Randomly select 5 from 1-15, 5 from 16-30 and so on... Then remove the numbers you selected from the list. This way you won't get any number twice.
Here's an example
//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]
//...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.