有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java试图通过使用嵌套循环获取非重复数字。。(二维阵列)

我试图找到一种方法,可以将随机数插入到一个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 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.
            }
        }
    }
}

共 (2) 个答案

  1. # 1 楼答案

    在宾果游戏中,你需要追踪列数,以获得每列的正确范围边界(下面代码中的最小值和最大值)

    对于列值的外部循环中的每次迭代,这些值都会相应地改变

    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);
    }
    

    输出

    [14, 22, 37, 57, 62]
    [15, 20, 45, 55, 63]
    [6, 29, 38, 52, 69]
    [11, 30, 34, 59, 74]
    [4, 16, 36, 54, 67]
    
  2. # 2 楼答案

    我建议你把它当作宾果游戏——你有一组数字,随机化,然后为棋盘选择数字。我相信宾果游戏的范围是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));
        }
    }