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 one for the rows and one for the col. And for every position I ask if it's 0 (I'm not sure how to make a control on that) and if it's 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;
}
Thanks in advance for everybody help.
Aucun commentaire:
Enregistrer un commentaire