在2D数组

时间:2016-06-21 15:01:55

标签: java

我正在尝试使用基于用户输入显示随机数的电路板。

例如:如果输入为3:

1 2 3

2 3 1

3 1 2

然而,我继续在2D数组中打印0作为我的值。 不确定有什么问题。感谢任何帮助。

输出:

0 0 0

0 0 0

0 0 0

这是我的代码:

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

    }

}

2 个答案:

答案 0 :(得分:0)

首先,为什么要交换生成随机矩阵? 您将方法交换输入作为0到2之间的整数传递 并且您将board[0-2][0-2]board[0-2][0-2]进行交换,这些内容始终为零。 因此,在交换之后,您将再次使用零结束!

您只需要:

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

答案 1 :(得分:0)

您需要初始化2D阵列,然后将其洗牌。代码可能是这样的:

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

在主函数中,插入如下:

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