Display data from a matrix in the form of characters

0

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("");
		}
	}
	
	
    
asked by M. Fer 12.12.2017 в 07:45
source

1 answer

1

If you have already declared a vector with the symbols you want to print, you only have to use the value of the matrix as an index of the vector when printing on the screen:

public static void cambiaSim (int tablero[][]){
    char[] simbolos={'@','?','#','*','~','.','ç','/','$'};

    for (int i=0;i<tablero.length;i++){
        for (int j=0;j<tablero[0].length;j++){
            System.out.println(simbolos[(tablero[i][j])%9]);
        }
        System.out.println(" ");
    }

I use%. This operation is called a module and returns the remainder of dividing one number by another. I make% 9 so that the result is less than 9 (which is the length of the symbol vector), so when looking for a position in the vector of symbols we will not leave it.

    
answered by 12.12.2017 / 08:16
source