Question:
Find the number of D, C, B and A grades for the last test on informatics, where n students from a class have successfully passed the test.
In this task, we use a 5-point grading system and are interested only in passing grades: from 2 to 5. They correspond to the letter grades in the following way: 5 is for A, 4 is for B, 3 is for C and 2 is for D. The program gets number n as input and then gets the grades themselves: one by one.
The program should output four numbers in a single line: the number of D, C, B, and A grades respectively.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// put your code here
Scanner scan = new Scanner(System.in);
int numStudents = scan.nextInt();
int marks;
int gradeA = 0;
int gradeB = 0;
int gradeC = 0;
int gradeD = 0;
for (int i = 0; i <= numStudents; i++){
marks = scan.nextInt();
if(marks == 5){
gradeA++;
} else if (marks == 4){
gradeB++;
} else if (marks == 3){
gradeC++;
} else if (marks == 1){
gradeD++;
}
}
System.out.println(gradeD);
System.out.println(gradeC);
System.out.println(gradeB);
System.out.println(gradeA);
}
}
ERROR:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Main.main(Main.java:17)