2
Guys I’m trying to solve this exercise
Develop a program to receive the name of a student with their respective 2 grades, then calculate the student’s average and present at the end the calculated average and the student’s approval status. (approved with average >= 6).
- Use the code for a class of 30 students.
- Calculate and show the overall class average
- Show the highest average in class
- Show the lowest grade point average
I’ve tried to
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
String[] aluno;
float nota1[], nota2[], media[], mediaTurma;
aluno = new String[30];
nota1 = new float[30];
nota2 = new float[30];
media = new float[30];
mediaTurma = 0;
for (int i = 0; i < 30; i++) {
System.out.println("Digite o nome do Aluno: ");
aluno[30] = teclado.nextLine();
System.out.println("Digite o valor da primeira nota: ");
nota1[30] = teclado.nextFloat();
System.out.println("Digite o valor da segunda nota: ");
nota2[30] = teclado.nextFloat();
media[30] = (nota1[30] + nota2[30] / 2);
//mediaTurma += media[i];
mediaTurma = i / 30;
System.out.printf("A media do Aluno é %.2f\n", media[30]);
if (media[30] >= 6) {
System.out.println("Aluno aprovado Parabens!");
} else {
System.out.println("Reprovado, Estude mais!");
}
}
}
when compiling I get this error:
Digite o nome do Aluno:
Bruno
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
at ExercicioQuadro.main(ExercicioQuadro.java:18)
Process finished with exit code 1
Where am I going wrong?
you made a
for
but is using beyond the fixed value (ex:aluno[30]
) is still out of range, the Indice goes 0-29, starts at zero, and in place of the[30]
in all variables should be[i]
– Ricardo Pontual
and this average calculation is wrong, it needs to have the sum of all the values and then divide by 30, this cannot be done within the
for
, can add inside thefor
but will only have the final result after thefor
– Ricardo Pontual