-2
I am trying, through the Scanner, to take the values typed by the user, to store in a vector and finally, to average the values typed, in the case the average of a student’s grades. However, I’m having this array exception outside the index, and I’m not able to identify what’s wrong with the code.Below is my class containing my methods and my class containing my main, respectively.
Class of methods
package classeexercicios;
public class MediaAluno {
double vetorNotas [] = new double[5];
double media;
double total = 0;
int i;
double receberNotas() {
if(vetorNotas[i] >= 0 || vetorNotas[i] <= 100) {
for(i = 0;i < vetorNotas.length;i++) {
total = total + vetorNotas[i];
if(vetorNotas[i] < 0 || vetorNotas[i] > 100) {
System.out.println("Insira uma nota valida entre o intervalo de 0 a 10");
}
}
}
return i;
}
double calcularMedia() {
total = total + vetorNotas[i];
return media = (total / vetorNotas.length);
}
}
Class containing the Main
package classeexercicios;
import java.util.Scanner;
public class MediaAlunoTeste {
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
MediaAluno nota = new MediaAluno();
for(int i = 0;i < 5;i++) {
System.out.print("Insira a nota do aluno:");
nota.vetorNotas[i] = teclado.nextDouble();
nota.receberNotas();
}
nota.calcularMedia();
System.out.println();
teclado.close();
}
}
Got it, tidied it up here and it worked. Thank you very much
– Marco Aurélio Lopes Junior