I'm trying to do a board which displays random numbers based on the user Input.
Eg: If input is 3:
1 2 3
2 3 1
3 1 2
However, i keep getting 0 printed as my values in the 2D array. Not sure what is wrong. Appreciate any kind assistance.
Output:
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. 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. 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:
createMatrixBoard(userInput);
fillBoard(matrixBoard);
shuffleBoard(matrixBoard);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.