I am trying to make a matrix in Java
that is going to be filled with random values only in that positions that are empty. I am trying these code but it doesn't work.
I made two nested for
loops, one for the rows and one for the columns. And for every position, I ask if it's 0
(I'm not sure how to make a control on that) and if it does not generate a random coordinate and print it in the format (i,j). But it is always printing 0
.
public int[][] getRandomPosition(int matrixlength) {
int[][] grid = new int[matrixlength][matrixlength];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 0) {
grid[i][j] = (int) Math.random();
System.out.print(grid[i][j]);
return grid;
}
}
}
return null;
}
Any idea of how I can resolve the problem at hand?