I've been thinking about the following problem for a few days: I have a square matrix of n by n elements created randomly by random math
public static void creaMatriz (int matriz [][],int d){
for (int i=0;i<matriz.length;i++){
for (int j=0;j<tablero[0].length;j++){
System.out.print ("["+tablero[i][j]+"]"+" ");
}
System.out.println("");
}
}
Within the same program, I have a method that gives numerical values to the matrix randomly (through math.random) the question I have is how could I get symbols on the screen ('!', '?' ...) instead of the numbers obtained in a random way, I mean, if a '1' had to appear, instead of that number a '?' appears, if it was a '2' a '!' appears.
One of the ideas I had was, when creating the matrix with the random numbers, that it would be 'populated' with ascii characters, but then I am already changing the functionality of the program, since it must operate with integers and, only when it comes to printing on the screen, to show characters (such as the ones mentioned '?', '@' ... and not the whole numbers.) Surely it is much simpler than the ideas I have had but I can not find it .
public static void cambiaSim (int tablero[][]){
char[] simbolos={'@','?','#','*','~','.','ç','/','$'};
char [][]tableroA= new char[9][9];
for (int i=0;i<tablero.length;i++){
for (int j=0;j<tablero[0].length;j++){
tableroA[i][j]=simbolos[(int)(Math.random()*9)];
System.out.println(tableroA[i][j]+" ");
}
System.out.println(" ");
}
I have also tried to traverse the matrix and change the data type by assigning it a char, but I can not print it correctly on the screen:
public static void cambiaTablero (int tablero [][]){
for (int i=0;i<tablero.length;i++){
for (int j=0;j<tablero[0].length;j++){
if (tablero[i][j]==0){
(tablero[i][j])=((char)('*'));
}else if (tablero[i][j]==1){
(tablero[i][j])=((char)('#'));
}else if (tablero[i][j]==3){
(tablero[i][j])=((char)('-'));
}else if (tablero[i][j]==4){
(tablero[i][j])=((char)('.'));
}else if (tablero[i][j]==5){
(tablero[i][j])=((char)('$'));
}System.out.print (tablero[i][j]);
}System.out.println("");
}
}