How do I display the names and notes of an array in java?

Asked

Viewed 65 times

-1

I’m a beginner in Java and decided to create this exercise to test my knowledge, but I’m breaking my head to show the values of this Array. I want him to show the name of the student at the end of the execution with his grades, I tried to make a fail-safe. If anyone can help me, I would be grateful! I want the following result at the end of the execution:

Name: Aluno1 Note1: Nota2:

Name: Aluno2 Note1: Nota2:

    Scanner entrada = new Scanner(System.in);

    double numero[] = new double[2];
    String nome[] = new String[2];

    for (int contador = 0; contador < nome.length; contador++) {
        System.out.println("Nome " + (contador + 1));
        nome[contador] = entrada.next();

        for (int cont = 0; cont < numero.length; cont++) {

            System.out.println("Informe a " + (cont + 1) + "° nota: ");
            numero[cont] = entrada.nextDouble();

        }

    }

    for (int cont = 0; cont < nome.length; cont++) {
        System.out.println("Nome: " + (cont + 1) + " " + nome[cont]);
        for (int conta = 0; conta < numero.length; conta++) {

            System.out.println("nota " + (conta + 1) + " " + numero[conta]);

        }

    }

}

}

1 answer

1

Buddy, note that in your code, after you add a student, you ask for the grades, right? Well, for the first case, it works. However, when entering the second student, as you are using the same "number" array, it overwrites the notes of the first student with that of the second student. There is no way to print separately, after insertion, the grades of the two students. They will always come out equal.

I suggest you either create another note array for the second student, or create a student class, which, within it, has this two note array. If you haven’t learned about classes yet, make another array to store the second student’s two notes.

Browser other questions tagged

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