简体   繁体   中英

Picking characters of vector and randomly placing them in a matrix

I have a String[50] that contains 50 characters, those characters need to be placed in a String[10][10] using (int)(Math.random() * 3) so the loop would go through the entire matrix placing the characters in the last position+whatever math.random gets. These questions that teachers ask that limits the way you can answer them drives me nuts.

    public static void main(String[] args) {
    Scanner entrada = new Scanner(System.in);
    System.out.println("Type a phrase.");
    String phrase = entrada.nextLine();
    while (frase.length() > 50) {
        System.out.println("The phrase has to be less than 50 characters. Try again.");
        phrase = entrada.nextLine();
    }
    int carac = phrase.length();
    String[] vector = new String[carac];
    for (int i = 0; i < carac; i++) {
        vector[i] = String.valueOf(phrase.charAt(i));
        System.out.print(vet[i]);
    }
    int position = 0;
    String[][] matrix = new String[10][10];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            position = (int) (Math.random() * 3);
            matrix[i][j] = vet[i] //this part doesn't make any sense, I'm stuck here.
        }
    }
}

Example of desired output:

在此处输入图片说明

The image shows that the characters from the input string need to be randomly distributed in the matrix.

So, a step can be calculated "randomly" (initially from 0 to 2, then from 1 to 3), and position in the matrix can be recalculated using division / and modulo % operations

Also, with this implementation array vet seems to be redundant, and vet[i] may be replaced with String.valueOf(phrase.charAt(i)) :

int position = 0;
for (int i = 0; i < carac; i++) {
    int row = position / matrix.length;
    int col = position % matrix[row].length;
    matrix[row][col] = vet[i]; // or just String.valueOf(phrase.charAt(i))
    position += (int)(Math.random() * 3) + 1; // shift by 1 position is needed here!
}

Then the matrix could be printed:

for (int i = 0; i < matrix.length; i++) {
    System.out.print(i + ": ");
    for (int j = 0; j < matrix[i].length; j++) {
        System.out.print((matrix[i][j] == null ? " " : matrix[i][j]));
    }
    System.out.println();
}

Sample output (with shift):

0:  TE  S T  
1: I  N G  S 
2:  E NT  EN 
3: C E       
4:           
5:           
6:           
7:           
8:           
9:           

Update Sample output ( without shift):

0: TSTI G EN 
1: T NC E    
2:           
3:           
4:           
5:           
6:           
7:           
8:           
9:           

It is evident that without the minimal shift of 1, lots of characters from phrase will get overwritten when put to the matrix and position = 0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM