5
"Make an algorithm to receive an integer n (number of students), an integer m (number of subjects), and nxm grades from 0 to 10, which each student obtained in each discipline. Present:
a) which (or which) subject(s) the students scored the highest average
b) which (or which) course(s) the students got the lowest average grade
c) which (or which) student(s) had the highest overall average d) which (or which) student(s) got the lowest overall average"
I thought I’d create a string for the students' names, another for the disciplines, and then create the matrix with the grades. Only then would I think of a way to calculate the average. From what I understand, I must print on the screen the names of the disciplines that correspond to the higher and lower averages; the same goes for the students. But I am unable to create such strings...
public static void main(String[] args) {
Scanner ent = new Scanner(System.in);
ent.useLocale(Locale.US);
int n, m, i, j;;
n = ent.nextInt(); // numero de alunos
m = ent.nextInt(); // numero de disciplinas
String [] a = new String[n]; // nomes dos alunos
String [] d = new String[m]; // nomes das disciplinas
double [][] M = new double[n][m]; // notas
// nomes dos alunos
for (i=0; i<n; i++) {
a[i] = ent.nextLine();
}
// nomes das disciplinas
for (j=0; j<m; j++) {
d[i] = ent.nextLine();
}
// monta tabela de notas: alunos X disciplinas
for (i=0; i<n; i++) {
for (j=0; j<m; j++) {
M[i][j] = ent.nextDouble();
}
}
// calcula a nota média de cada disciplina
double soma=0, media=0;
for (j=0; j<m; j++) {
for (i=0; i<n; i++) {
soma = soma + M[i][j];
}
media = soma/n;
System.out.println("Média de "+d[j]+": "+media);
}
}
When I run the program and type the name of the first student or type all and then enter, the following message appears:
Exception in thread "main" java.util.Inputmismatchexception at
java.util.Scanner.throwFor(Scanner.java:909) at
java.util.Scanner.next(Scanner.java:1530) at
java.util.Scanner.nextInt(Scanner.java:2160) at
java.util.Scanner.nextInt(Scanner.java:2119) at
aula11.teste.main(test.java:8) Java Result: 1
I don’t know what you mean or what I should do to make it right.
Because the
ent.useLocale(Locale.US);
?– Victor Stafusa
Because I use dot to separate decimals. Using comma gives error.
– Cristiane Dos Santos Costa
I still don’t know what the problem is with the
Scanner
, but in calculating the average you will need to putsoma = 0;
at the beginning offor
that iterates thej
in the end, but before thefor
intern.– Victor Stafusa
Or else move the declaration of the
soma
and ofmedia
to the beginning offor
that is just below.– Victor Stafusa
What is line 8 in your program? It reads the
n
?– Victor Stafusa
Okay, I’ll arrange that, thanks for the tip. For now my problem is creating the strings, otherwise I have no way to display the names of students and subjects of higher and lower average.
– Cristiane Dos Santos Costa
Yes, line 8 reads n.
– Cristiane Dos Santos Costa
Let’s go continue this discussion in chat.
– Cristiane Dos Santos Costa