-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercentToGPA.java
29 lines (28 loc) · 910 Bytes
/
PercentToGPA.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
import java.util.Scanner;
public class PercentToGPA {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Percentage: ");
int percentage = scanner.nextInt();
double GPA = getGPA(percentage);
if (GPA != -1) {
System.out.println("Percentage: " + percentage + "%, Your GPA is: " + GPA);
} else {
System.out.println("Invalid % entered.");
}
scanner.close();
}
private static double getGPA(int percentage) {
double GPA;
if (percentage >= 95) {
GPA = 4.0;
} else if (percentage >= 65) {
GPA = 1 + (percentage - 65) / 10.0;
} else if (percentage == 0) {
GPA = 0.0; // Set GPA to 0.0 for 0%
} else {
GPA = -1; // throw error if invalid
}
return GPA;
}
}