I'm doing exercises on composite variables and I got stuck in my code, which at first I can not seem to solve even though it seems to be something simple.
At the time of the result display, the student's notes are not being displayed individually, but the sum of all, ie: Student 1 displays the notes correctly, Student 2 displays the notes of 1 in addition to their own, and 3 displays the previous ones and his.
I need to know how I can show all the notes of each student individually, without showing the previous ones.
I imagine the problem is in the way I defined the variable "register2"
Follow 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);
}