1
I intend to create a grid that is filled randomly or with "W" or "_" and I did the following:
public class Gerador_grelha {
public static void main(String[] args) {
int row = 5;
int col = 5;
String[][] grid = new String[row][col];
String AB = "_W";
SecureRandom rnd = new SecureRandom();
for (int i = 0; i < grid.length; i++) {
StringBuilder sb = new StringBuilder(row);
for (int j = 0; j < grid[i].length; j++) {
sb.append(AB.charAt(rnd.nextInt(AB.length())));
sb.toString();
grid[i][j] = sb.toString();
}
}
for(String[] row:grid) {
for(String c:row) {
System.out.print(c);
}System.out.println();
}
}
But in this case it would be a 5 by 5 grid I’m getting one like this:
WW_W_WW_W_W_W__
_____W__W___W__
__W_WW_WWW_WWW_
__W_WW_WW__WW_W
______________W
Which is clearly not 5 by 5. Can anyone help solve the problem?
thank you, indeed the mistake was that.
– MDordio