Arrays in JAVA

Asked

Viewed 300 times

1

I want to make a school report in JAVA, a matrix with 4 lines that will be the number of students,and 4 columns that will be the 4 grades of each student I used a vector to pick up the average at the end, my only problem is that I wish that first all students put their grades to only at the end print on the average screen of each one. I don’t know what the problem with my code is,".

public static void main(String[] args) {

    final int TOTAL_ALUNOS = 4;
    final int TOTAL_BIMESTRES = 4;
    final double NOTA_MINIMA = 70.0;

    Scanner input = new Scanner(System.in);
    String[] nomeAlunos = new String[TOTAL_ALUNOS];
    double[][] notaAlunos = new double[TOTAL_ALUNOS][TOTAL_BIMESTRES];
    double[] mediaAlunos = new double[TOTAL_ALUNOS];

    //Obter nome dos alunos
    for (int i = 0; i < TOTAL_ALUNOS; ++i) {
        System.out.println("Informe o nome do " + (i + 1) + "° aluno:");
        nomeAlunos[i] = input.nextLine();

        System.out.println("");

        //Obter notas dos alunos todos os bimetres
        for (int y = 0; y < TOTAL_ALUNOS; ++y) {
            for (int j = 0; j < TOTAL_BIMESTRES; ++j) {
                System.out.println("Informe a nota do aluno " + nomeAlunos[y]
                        + " para o " + (j + 1) + "° bimestre");
                notaAlunos[y][j] = input.nextDouble();
            }
        }
    }

    //calcular media alunos
    for (int i = 0; i < TOTAL_ALUNOS; ++i) {
        for (int j = 0; j < TOTAL_BIMESTRES; ++j) {
            mediaAlunos[i] += notaAlunos[i][j];
        }
        mediaAlunos[i] /= TOTAL_BIMESTRES;
    }

    //Mostrar situacao dos alunos
    System.out.println("======== RESULTADO FINAL =======");

    for (int i = 0; i < TOTAL_ALUNOS; ++i) {

        if (mediaAlunos[i] >= NOTA_MINIMA) {
            System.out.println("Nome: " + nomeAlunos[i] + " Media: " + mediaAlunos[i]
                    + " Situação: Aprovado");
            continue;
        }

        System.out.println("Nome: " + nomeAlunos[i] + " Media: " + mediaAlunos[i]
                + " Situação: Reprovado");
    }

}

}

1 answer

3

In consecutive reading of numeric values and String must empty the keyboard buffer before reading the String value, for example:

int n;
String s;

System.out.printf("Informe um Número Inteiro: ");
n = ler.nextInt();

ler.nextLine(); // esvazia o buffer do teclado

System.out.printf("Informe uma cadeia de caracteres:\n");
s = ler.nextLine();
  • 1

    It worked, thank you very much Natan!

  • Just did not understand one thing,worked very well now,but I only emptied after input of the "name",I did not use after the user put the notes,but is not accumulating value on top of the other, could you explain me? because you said that in consecutive reading of numerical values should empty.

  • 1

    because using the read.nextLine(); in the next interaction of the for it will also clear the numeric values. I am happy to help.

Browser other questions tagged

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