[英]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.