简体   繁体   English

在2D数组中生成唯一的随机数

[英]Generate unique random numbers in 2D array

I'm trying to do a board which displays random numbers based on the user Input. 我正在尝试制作一个基于用户输入显示随机数的面板。

Eg: If input is 3: 例如:如果输入为3:

1 2 3 1 2 3

2 3 1 2 3 1

3 1 2 3 1 2

However, i keep getting 0 printed as my values in the 2D array. 但是,我一直在2D数组中将0打印为我的值。 Not sure what is wrong. 不知道出什么问题了。 Appreciate any kind assistance. 感谢任何帮助。

Output: 输出:

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Here is my code: 这是我的代码:

Public class Mb {

    /**
     * @param args the command line arguments
     */

    public static int[][] createMatrixBoard(int size)
    {
        int[][] board = new int[size][size];
        return board;
    }

    public static void printMatrixBoard(int[][] board)
    {
        for(int i = 0; i<board.length; i++)
        {
            for(int j = 0; j<board[i].length; j++)
            {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }

     public static void shuffleBoard(int[][] board)
    {
        Random rnd = new Random();
        int randX = 0;
        int randY = 0;

        for(int x = 0; x<board.length; x++) //no of rows
        {
            for(int y = 0; y<board[x].length; y++) //x refers to no of columns in each row
            {
                randX = rnd.nextInt(board.length);
                randY = rnd.nextInt(board.length);
                swap(board, x, y, randX, randY);
            }
        }
    }

    public static void swap(int[][] board, int x1, int y1, int x2, int y2)
    {
        int temp = board[x1][y1]; //use temp variable to store original value of one of the elemnt
        board[x1][y1] = board[x2][y2]; //swap the value position
        board[x2][y2] = temp; //swap the remaining value position
    }

    public static void main(String[] args) {
        // TODO code application logic here

        int userInput = 0;


        System.out.print("Matrix size: ");
        Scanner scn = new Scanner(System.in);
        userInput = scn.nextInt();
        while(userInput < 0 && userInput > 9)
        {
            System.out.println("Invalid matrix size. Re-enter ");

        }

        int[][] matrixBoard = new int[userInput][userInput];
        createMatrixBoard(userInput);
        shuffleBoard(matrixBoard);
        printMatrixBoard(matrixBoard);

    }

}

First of all why are you swapping to generate a random matrix ? 首先,为什么要交换以生成随机矩阵? You are passing your method swap inputs as integers between 0 to 2 and you are swapping board[0-2][0-2] with board[0-2][0-2] , which were zeroes always. 您将方法交换输入作为0到2之间的整数进行传递,并且将board[0-2][0-2]board[0-2][0-2]交换,始终为零。 So after swapping you will end up a board with zeroes again ! 因此,交换之后,您将再次获得零板!

All you need is : 所有你需要的是 :

        for(int x = 0; x<board.length; x++) //no of rows
        {
            for(int y = 0; y<board[x].length; y++) //x refers to no of columns in each row
            {
                board[x][y] = 1 + rnd.nextInt(board.length);
            }
        }

You need initialize the 2D array, then shuffle it. 您需要初始化2D数组,然后将其随机播放。 The code may like this: 代码可能像这样:

public static void fillBoard(int[][] board) {
    for (int x = 0; x < board.length; x++) {
        for (int y = 0; y < board[x].length; y++) {
            if (y==0) board[x][y] = x + 1;
            else board[x][y] = board[x][y-1] % board.length + 1;
        }
    }
}

public static void shuffleBoard(int[][] board)
{
    Random rnd = new Random();
    int randX = 0;

    for(int x = 0; x<board.length; x++) //no of rows
    {
        randX = rnd.nextInt(board.length);
        int[] temp = board[x];
        board[x] = board[randX];
        board[randX] = temp;
    }
}

and in your main function, insert it like this: 然后在您的main函数中将其插入,如下所示:

createMatrixBoard(userInput);
fillBoard(matrixBoard);
shuffleBoard(matrixBoard);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM