Problem printing the contents of a vector

Asked

Viewed 276 times

1

How to make a score accumulator for each answer.

package modulo02;

import java.util.Scanner;

public class vetorGabarito {
    public static void main(String[] args) {
        int pontuacao = 0, n, i, j = 0;
        float s = 0, maiorMedia = -1;
        Scanner sc = new Scanner(System.in);

        System.out.println("Quantos alunos há na turma?: ");
        n = sc.nextInt();

        String nome[] = new String[n];
        char resposta[][] = new char[n][3];
        float media[] = new float[n];
        char gabarito[] = new char[3];

        for (int g = 0; g < gabarito.length; g++) {
            System.out.println("Gabarito questão[" + g + "]: ");
            gabarito[g] = sc.next().charAt(0);
        }

        for (i = 0; i < n; i++) {
            System.out.println("Nome do aluno: ");
            nome[i] = sc.next();



            for (j = 0; j < 3; j++) {
                System.out.println("Resposta nº " + (j + 1));
                resposta[i][j] = sc.next().charAt(0);


                if (resposta[i][j] == gabarito[j]) {

                    pontuacao++;
                }


            }

            System.out.println("Pontuação: " + pontuacao);


        }

        System.out.println("Relatório geral: ");
        System.out.println("resposta - aluno");


        for (i = 0; i < n; i++) {
            System.out.print(nome[i]);



        }

        for (j = 0; j < n; j++) {
            System.out.println(resposta[j]);




            }




    }
}

sdsa

I wanted the names to be under Students and the answers under Answers.

  • What is the relation of the question with swing?

1 answer

2


EDIT: Tanto char[] x = new char[n], how much char x[] = new char[n] are compiled normally, as @Diego Felipe said. However, it is preferable that the second form be avoided, according to official documentation (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html).

Doubt 1:

You already have a variable q representing all response sets within that loop for, then you could take this to make a matrix, ie a vector vector, for your respostaVetor, that would look like this:

char[][] respostaVetor = new char[n][10];

That way, when it comes time to save the answers, you’d just have to do:

respostaVetor[q][i] = sc.next().charAt(0);

And then, to read, a similar loop, which accesses a student’s response group and then the response itself:

System.out.println("\nResposta nº" + q + " \nQuestão[" + i + "]: " + respostaVetor[q][i]);

Doubt 2:

To save each student’s score individually, you would only need one more vector of ints, something like

int[] pontuacao = new int[n];
  • 1

    Whatever between the two forms you pointed out. Pro java, this is indifferent if you do tipo variavel[] or tipo[] variavel, it recognizes as vector in the same way. It is no problem to do in either of the two forms.

  • @Mrpingu I made an array as you said, now I appeared another problem, when I will display the names and the answers, for you to better understand I put a photo of my stack trace in the post.

  • @Mrpingu The first question I have already solved with your help, now the scoring part and how I’m going to show it is that I still have problem. And also the way to show names and answers still do not know how to solve, is it because I am using 2 for? One for names and one for answers, shouldn’t I use just a for? to show everything? I’ve tried everything, but I haven’t been able to.

  • The idea of using two loops for is correct, you just have to put the answers inside the loop that goes through the names, instead of separate, and then displays as System.out.print(nome[i] + " - " resposta[j]). In the case of punctuation, try to do as I described, another vector with the same number of elements as that of students (n, in case) and add the score in if (resposta[i][j] == gabarito[j]) with pontuacao[i]++, being i the position of that student in the vector.

Browser other questions tagged

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