Java 尝试使用嵌套循环..(二维数组)获取非重复数字
我试图找到一种方法,可以将随机数插入到一个5x5的二维数组中。我正在制作一款宾果游戏,如果有人能帮我,我将不胜感激。我对Java相当陌生,所以任何帮助都会很好。谢谢 考虑:Java 尝试使用嵌套循环..(二维数组)获取非重复数字,java,arrays,Java,Arrays,我试图找到一种方法,可以将随机数插入到一个5x5的二维数组中。我正在制作一款宾果游戏,如果有人能帮我,我将不胜感激。我对Java相当陌生,所以任何帮助都会很好。谢谢 考虑: boolean matchFound; // used to track when a repeat is found int rand; // temporarily store the random number before putting it into the array to check if it's a r
boolean matchFound; // used to track when a repeat is found
int rand; // temporarily store the random number before putting it into the array to check if it's a repeat
Random rn = new Random();
for (int i = 0; i < 25 ; i++) {
rand = rn.nextInt(15) + 1;
matchFound = false; // assume that the number generated is not a repeat
for (int x = 0; x <= 5; x++) {
for (int j = 0; j <= 5 ; ++j){
if (rand == b[x][j]) { // check if the number just generated is a repeat
matchFound = true; // if it is, set the boolean to True and use that after the loop
}
if (matchFound == true) { // if the last number is a repeat, then
i = i - 1; // reduce the loop counter by 1 to go back to that place for the new number
} else {
b[i][j] = rand; // the number was not repeated so insert it.
}
}
}
}
boolean matchFound;//用于跟踪何时发现重复
int rand;//临时存储随机数,然后再将其放入数组中,以检查它是否重复
Random rn=新的Random();
对于(int i=0;i<25;i++){
rand=rn.nextInt(15)+1;
matchFound=false;//假设生成的数字不是重复数
对于(intx=0;x欢迎来到SO社区
在宾果游戏中,您需要跟踪列号以获得每个列的正确范围边界(下面代码中的最小值和最大值)
对于列值的外部循环中的每个迭代,这些值将相应地改变
public static void main(String[] args) {
int[][] b = new int[5][5]; // 5x5 grid
for (int i = 0; i < 5; i++) { // loop for columns
int min = i * 15 + 1;
int max = (i + 1) * 15;
for (int j = 0; j < 5; j++) { // loop for rows
int rand;
do {
rand = getRandom(min, max); // generate random int between min & max values
} while (isMatch(rand, b));
b[j][i] = rand;
}
}
/* print array values */
for (int[] x : b)
System.out.println(Arrays.toString(x));
}
/* return true if num is in the array b */
private static boolean isMatch(int num, int b[][]) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (num == b[i][j]) {
return true;
}
}
}
return false;
}
/* return integer number between min & max inclusive */
public static int getRandom(int min, int max) {
return (int) ((Math.random() * (max + 1 - min)) + min);
}
我建议将其视为一个宾果游戏——你有一组数字,将它们随机排列,然后为棋盘“选择”数字。我相信宾果的范围是1-75?但你可以改变它。然后从你的ArrayList中选择前25个数字,它们肯定是随机的,不会以这种方式重复
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Test {
public static void main(String[] args){
ArrayList<Integer> bingo = new ArrayList<Integer>();
int[][] b = new int[5][5];
// create a list in order
for (int i = 1; i <= 75; i++) {
bingo.add(i);
}
Collections.shuffle(bingo); // randomize
// Add to the board
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
b[i][j] = bingo.remove(0); // remove the first item in the shuffled list
}
}
System.out.print(Arrays.deepToString(b));
}
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
公开课考试{
公共静态void main(字符串[]args){
ArrayList bingo=新建ArrayList();
int[]b=新的int[5][5];
//按顺序创建一个列表
对于(int i=1;i Hello,在数组中从1添加到15是不适用的,因为您需要25个不同的元素,因此您将得到无穷大的loopAh是的,这可以解释这一点,但是如果我想在二维数组中添加第一列[1][5]对于宾果游戏,1-15秒,16-30…等等。最多75。你有什么建议吗?哇,非常感谢。它工作得很好,但我想了解开关和大小写断开的作用。只是这样我不觉得我只是在编译代码,我不知道它是如何工作的。所以如果你能做一个简短的解释,那就太好了!再次感谢!高兴对于那个人,我只是再次修改了代码,并删除了这个开关,使代码更加紧凑…开关语句就像if else语句一样,它检查输入,如果它与情况匹配,那么它会执行一些操作并退出开关状态。好的,你帮了大忙!谢谢。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Test {
public static void main(String[] args){
ArrayList<Integer> bingo = new ArrayList<Integer>();
int[][] b = new int[5][5];
// create a list in order
for (int i = 1; i <= 75; i++) {
bingo.add(i);
}
Collections.shuffle(bingo); // randomize
// Add to the board
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
b[i][j] = bingo.remove(0); // remove the first item in the shuffled list
}
}
System.out.print(Arrays.deepToString(b));
}
}