Sorting letters in an array, amount entered by the user

0

I'm having difficulty here so the user can type how many letters each of them wants to be drawn. Example as in the code below shows the letters char[] letras = new char[]{'S', 'C', 'M', 'A','L'}; . These letters are drawn in the array, but I want to make the user type how many S , how C , how M , and so on. My code is the one below ..

public class exemplo {


    public static void main(String[] args) {

        int tamanho = 4; 

        char[] letras = new char[]{'S', 'C', 'M', 'A','L'};
        char matrix [][] = new char[tamanho][tamanho];


        Random random = new Random();
        for (int i = 0; i < tamanho; i++) {
            for (int j = 0; j < tamanho; j++) {

                matrix[i][j] = letras[random.nextInt(letras.length)];

            }

        }
        for (int i = 0; i < tamanho; i++) {
            for (int j = 0; j < tamanho; j++) {
                System.out.print("|" + matrix[i][j] + "|");
            }

       System.out.println("");
    }
    }
}
    
asked by anonymous 02.08.2018 / 19:24

1 answer

0

Hello, well, I do not know if it's exactly what you were looking for but that was my solution,

First I created a list of characters to store all the possible letters to be drawn,

List<Character> base_sorteio = new ArrayList<>();

Then I looped to ask the user the desired amount of each letter using the Scanner class, then I add the character base to the amount entered by the user for that particular character,

    int y = 0;
    Scanner sc = new Scanner(System.in);
    for (int i = 0; i < letras.length; i++) {

        System.out.println("Informe a quantidade de '" + letras[i] + "'");

        int k = sc.nextInt();
        for (int f = 0; f < k; f++) {
            base_sorteio.add(letras[i]);
        }
    }

After entering the amount of each letter, the list of characters will store something like:

[S, C, C, C, C, M, M, M, M, M, A, A, A, L, L, L]

In the case of the sima, I typed 1S, 4C, 5M, 3A and 3L. Then I go over the list,

Collections.shuffle(base_sorteio); 

It looks like this:

[M, M, C, C, A, A, L, C, L, M, C, M, S, A, L, M]

After that, just add this list to the array:

    y = 0;
    for (int i = 0; i < tamanho; i++) {
        for (int j = 0; j < tamanho; j++) {
            matrix[i][j] = base_sorteio.get(y);
            y++;
        }
    }

Here is the complete code:

import java.util.*;

public class exemplo {

    public static void main(String[] args) {

        int tamanho = 4;

        char[] letras = new char[]{'S', 'C', 'M', 'A', 'L'};
        char matrix[][] = new char[tamanho][tamanho];

        List<Character> base_sorteio = new ArrayList<>();

        int y = 0;
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < letras.length; i++) {

            System.out.println("Informe a quantidade de '" + letras[i] + "'");

            int k = sc.nextInt();
            for (int f = 0; f < k; f++) {
                base_sorteio.add(letras[i]);
            }
        }

        Collections.shuffle(base_sorteio);

        y = 0;
        for (int i = 0; i < tamanho; i++) {
            for (int j = 0; j < tamanho; j++) {
                matrix[i][j] = base_sorteio.get(y);
                y++;
            }
        }
        System.out.println("");
        for (int i = 0; i < tamanho; i++) {
            for (int j = 0; j < tamanho; j++) {
                System.out.print("|" + matrix[i][j] + "|");
            }

            System.out.println("");
        }
    }
}

I hope I have helped;)

    
19.08.2018 / 02:27