Home › Java Tutorial › Java project: grade report
Java project: grade report
Combine arrays, loops and methods.
This small program uses an array, a method, a loop and formatted output. Try adding a name for each score, or printing a letter grade with if / else.
Example code
public class Main {
static double average(int[] a) {
int sum = 0;
for (int n : a) sum += n;
return (double) sum / a.length;
}
public static void main(String[] args) {
int[] scores = {80, 92, 67};
int best = scores[0], passed = 0;
for (int s : scores) {
if (s > best) best = s;
if (s >= 70) passed++;
}
System.out.printf("Average: %.2f%n", average(scores));
System.out.println("Highest: " + best);
System.out.println("Passed: " + passed);
}
}