-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradeCalculator.java
67 lines (50 loc) · 2.66 KB
/
GradeCalculator.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Scanner;
import java.lang.Math;
public class GradeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Welcome user and explain functionality
System.out.println("Welcome to Alesandra's Grade Calculator!\nTo see instructions, please type 'help' or type any character and hit enter to get started.");
String input = scanner.nextLine();
if (input.equals("help")){
System.out.println("\nBegin by entering your grade (%) on an assessment, followed by its respective weight (%).\nRepeat this until you have entered all entities and type 'done' to indicate this.");
System.out.println("\nFrom here you will have the option to calculate the remaining grade you must recieve, to acheive your target grade.\n");
}
float totalWeight = 0;
float totalGradeWeighted = 0;
// Collect user input until they input 'done'
while (true){
System.out.println("Please enter your grade, or type 'done' to indicate you are finished: ");
input = scanner.nextLine();
if (input.equals("done")){
break;
}
float grade = Float.parseFloat(input);
System.out.println("Please enter the corresponding weight: ");
float weighting = Float.parseFloat(scanner.nextLine());
totalWeight += weighting;
totalGradeWeighted += grade * weighting;
}
if (totalWeight > 0 && totalWeight == 100){
float current = totalGradeWeighted/totalWeight;
System.out.println("Grade: " + current);
}
if (totalWeight > 0 && totalWeight < 100){
float current = totalGradeWeighted/totalWeight;
System.out.println("Grade: " + current);
// prompt and calculate required grade to reach target grade
if (totalWeight != 100){
System.out.print("Please enter your target grade: ");
float target = Float.parseFloat(scanner.nextLine());
float decTotalWeight = (totalWeight)/100;
float decFinalWeight = (100 - totalWeight)/100;
float required = (target - (decTotalWeight * current))/(decFinalWeight);
System.out.println("To finish with a " + target + " you must achieve a grade of " + required + " on your final exam.");
}
}
else {
System.out.println("Sorry, unable to calculate final grade. No grades entered.");
}
scanner.close();
}
}