Doubt about sum of information

Asked

Viewed 33 times

0

I’m doing some exercises on composite variables and I’ve reached an impasse in my code, which at first I can’t solve despite it seems to be something simple.

At the time of the display of the result, students' grades are not being displayed individually, but rather the sum of all, i.e.: Student 1 displays the notes correctly, Student 2 displays the notes of 1 in addition to its own, and Student 3 displays the previous ones and his.

I need to know how to show all the grades of each student individually, without showing the previous ones.

I imagine the problem is in the way I defined the variable "registromostrar2"

Follows the code:

import javax.swing.JOptionPane;

public class exercicio3{

    static final int NOTAS = 6;

    static class ALUNO {
        String Nome, Curso;
        int Semestre, Sala;
        double Notas [] = new double [NOTAS];
    }

    public static void main(String args[]){

        int alunos = Integer.parseInt(JOptionPane.showInputDialog("Insira a quantidade de alunos que irá registrar."));
        ALUNO Alunos [] = new ALUNO [alunos];
        int i = 0;
        int j = 0;

        for(i = 0; i < alunos; i++){
            int numaluno = i+1;
            Alunos[i] = new ALUNO ();
            Alunos[i].Nome = JOptionPane.showInputDialog("Insira o nome do aluno " + numaluno + ":");
            Alunos[i].Curso = JOptionPane.showInputDialog("Insira o curso do aluno " + numaluno + ":");
            Alunos[i].Semestre = Integer.parseInt(JOptionPane.showInputDialog("Insira o semestre(1 ou 2) do aluno " + numaluno + ":"));
            Alunos[i].Sala = Integer.parseInt(JOptionPane.showInputDialog("Insira a Sala do aluno " + numaluno + ":"));

            for(j = 0; j < NOTAS; j++){
                int qualnota = j+1;
                Alunos[i].Notas[j] = Double.parseDouble(JOptionPane.showInputDialog("Insira a " + qualnota + " nota do aluno " + numaluno + ":"));
            }

        }


        String registromostrar1 = "";
        String registromostrar2 = "";
        String registromostrar3 = "";

        for(i = 0; i < alunos; i++){
            int numaluno = i+1;
            for(j = 0; j < NOTAS; j++){
                registromostrar2 +=  Alunos[i].Notas[j] + " ; ";
            }

            registromostrar1 = "Dados do aluno " + numaluno + ":" + "\nNome: " + Alunos[i].Nome + "." +
            "\nCurso: " + Alunos[i].Curso + "." +
            "\nSemestre: " + Alunos[i].Semestre + "." +
            "\nSala: " + Alunos[i].Sala + "." +
            "\nNotas: " + registromostrar2;

            registromostrar3 += registromostrar1 + "\n\n\n";

        }

        JOptionPane.showMessageDialog(null, registromostrar3);





    }

1 answer

0


The problem is simple, you forgot to reset your variable registromostrar2

import javax.swing.JOptionPane;

public class JavaApplication5 {

    /**
     * @param args the command line arguments
     */

    static final int NOTAS = 6;

    static class ALUNO {
        String Nome, Curso;
        int Semestre, Sala;
        double Notas [] = new double [NOTAS];
    }
    public static void main(String[] args) {
        // TODO code application logic here

        int alunos = Integer.parseInt(JOptionPane.showInputDialog("Insira a quantidade de alunos que irá registrar."));
        ALUNO Alunos [] = new ALUNO [alunos];
        int i = 0;
        int j = 0;

        for(i = 0; i < alunos; i++){
            int numaluno = i+1;
            Alunos[i] = new ALUNO ();
            Alunos[i].Nome = JOptionPane.showInputDialog("Insira o nome do aluno " + numaluno + ":");
            Alunos[i].Curso = JOptionPane.showInputDialog("Insira o curso do aluno " + numaluno + ":");
            Alunos[i].Semestre = Integer.parseInt(JOptionPane.showInputDialog("Insira o semestre(1 ou 2) do aluno " + numaluno + ":"));
            Alunos[i].Sala = Integer.parseInt(JOptionPane.showInputDialog("Insira a Sala do aluno " + numaluno + ":"));

            for(j = 0; j < NOTAS; j++){
                int qualnota = j+1;
                Alunos[i].Notas[j] = Double.parseDouble(JOptionPane.showInputDialog("Insira a " + qualnota + " nota do aluno " + numaluno + ":"));
            }

        }


        String registromostrar1 = "";
        String registromostrar2 = "";
        String registromostrar3 = "";

        for(i = 0; i < alunos; i++){
            int numaluno = i+1;
            registromostrar2 ="";
            for(j = 0; j < NOTAS; j++){

                registromostrar2 +=  Alunos[i].Notas[j] + " ; ";
            }

            registromostrar1 = "Dados do aluno " + numaluno + ":" + "\nNome: " + Alunos[i].Nome + "." +
            "\nCurso: " + Alunos[i].Curso + "." +
            "\nSemestre: " + Alunos[i].Semestre + "." +
            "\nSala: " + Alunos[i].Sala + "." +
            "\nNotas: " + registromostrar2;

            registromostrar3 += registromostrar1 + "\n\n\n";

        }

        JOptionPane.showMessageDialog(null, registromostrar3);
    }

}

It follows highlighted the part of the code that you need to reset so that the other student’s "accumulation" does not occur :

 for(i = 0; i < alunos; i++){
        int numaluno = i+1;
    //Linha adicionada
    registromostrar2 ="";
        for(j = 0; j < NOTAS; j++){

            registromostrar2 +=  Alunos[i].Notas[j] + " ; ";
        }

        registromostrar1 = "Dados do aluno " + numaluno + ":" + "\nNome: " + Alunos[i].Nome + "." +
        "\nCurso: " + Alunos[i].Curso + "." +
        "\nSemestre: " + Alunos[i].Semestre + "." +
        "\nSala: " + Alunos[i].Sala + "." +
        "\nNotas: " + registromostrar2;

        registromostrar3 += registromostrar1 + "\n\n\n";

    }
  • Don’t forget to rate the answer positively if it has helped you!

  • Thanks, I can’t believe that was it hahaha! Thanks :3

Browser other questions tagged

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