Java Tutorial
Java Project: Grade Calculator
Convert numeric scores to letter grades, compute a GPA-style average, and print a report.
What you will build
Map a number to a letter, then average the scores.
Letters and average
Example
public class Main {
static String letter(int n) {
if (n >= 90) return "A";
if (n >= 80) return "B";
if (n >= 70) return "C";
if (n >= 60) return "D";
return "F";
}
public static void main(String[] args) {
int[] scores = {91, 84, 73, 68};
int sum = 0;
for (int n : scores) {
System.out.println(n + " " + letter(n));
sum += n;
}
System.out.printf("average: %.1f%n", sum / (double) scores.length);
}
}Practice
- Compile the complete program at
/java/try. - Change one input value and compile again.
- Replace a hardcoded number with
Scannerwhen you want typed input.