-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculateGPA.java
34 lines (31 loc) · 1020 Bytes
/
CalculateGPA.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
public class CalculateGPA {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
System.out.println("Enter your grade (A to F):");
String grade = input.nextLine().toUpperCase();
try {
double gpa = calculateGPA(grade);
System.out.println("Your GPA is: " + gpa);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
public static double calculateGPA(String grade) {
switch (grade) {
case "A":
return 4.0;
case "B":
return 3.0;
case "C":
return 2.0;
case "D":
return 1.0;
case "F":
return 0.0;
default:
throw new IllegalArgumentException("Invalid grade. Please enter a grade from A to F.");
}
}
}