Draw letters in a matrix, quantida typed by the user

Asked

Viewed 209 times

3

I am having difficulty here to be able to type how many letters of each one it 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 matrix, but I want to make the user type how many S, how many C, how many M and so on. My code is this 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("");
    }
    }
}
  • What would happen if the sum of S, C, M, A and L given by the user is different from 16?

  • well thought out, would make a big mistake

  • what you would recommend me to do?

  • It depends. What do you want to do with it? Why do you want to do this?

  • to make an agent, distribute letters in the matrix for the agent to search for the dirt and clean...

1 answer

2

Hello, good, I don’t know if this is exactly what you were looking for but this was my solution,

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

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

After I made a loop to ask the user for the desired amount of each letter using the Scanner class, then I add the character base 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 inserting the amount of each letter, the character list will store something like:

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

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

Collections.shuffle(base_sorteio); 

being 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 matrix:

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

Below 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("");
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.