86 lines
2.6 KiB
Java
86 lines
2.6 KiB
Java
|
||
/**
|
||
* CS115
|
||
*/
|
||
import java.util.Scanner;
|
||
|
||
public class GradeCalculatorProject {
|
||
|
||
/* Print specifications – method separate from main */
|
||
public static void main(String[] args) {
|
||
|
||
final int MAX_GRADES = 10; // the max arrau length
|
||
final int QUIT = 999; // to quit
|
||
int[] grades = new int[MAX_GRADES]; // array of grades
|
||
int count = 0; // how many grades the user entered
|
||
int total = 0; // calculate the total of grades
|
||
|
||
printSpecifications(); // specifications
|
||
Scanner sc = new Scanner(System.in);
|
||
|
||
/* Main input loop – stop either on 999 or after 10 grades */
|
||
System.out.print("Enter quiz grade (or 999 to finish): ");
|
||
while (count < MAX_GRADES) {
|
||
int grade = sc.nextInt();
|
||
|
||
/* Stop if user typed 999 */
|
||
if (grade == QUIT) {
|
||
System.out.println("...Quitting…");
|
||
break;
|
||
}
|
||
|
||
if (grade < 0 || grade > 100) {
|
||
System.out.println("Invalid grade – must be 0‑100. Try again.");
|
||
continue; // don't count this iteration
|
||
}
|
||
|
||
grades[count] = grade; // store the grade
|
||
total += grade; // addition while looping
|
||
count++; // increment number of grades as long as they dont exceed 10.
|
||
|
||
System.out.print("Enter next quiz grade (or 999 to finish): ");
|
||
}
|
||
|
||
sc.close();
|
||
|
||
/* Print all grades entered */
|
||
System.out.println("\n=== All Grades Entered ===");
|
||
for (int i = 0; i < count; i++) {
|
||
System.out.println("Grade #" + (i + 1) + " : " + grades[i]);
|
||
System.out.println("");
|
||
}
|
||
|
||
/* Compute average */
|
||
double average = 0.0;
|
||
if (count > 0) {
|
||
average = (double) total / count; // we cast the total since the total is an int
|
||
}
|
||
System.out.printf("\nAverage of %d grades is: %.2f%n", count, average);
|
||
char letterGrade = determineLetterGrade(average);
|
||
System.out.println("Letter grade: " + letterGrade);
|
||
System.out.println("===== End =====");
|
||
}
|
||
|
||
private static void printSpecifications() {
|
||
System.out.println("\n===== Quiz Grade Calculator =====");
|
||
System.out.println("This program will take 10 quiz grades from 0‑100,");
|
||
System.out.println("Once all the ten inputs are entered, your grades will be printed,");
|
||
System.out.println("and the program will compute your average score and letter grade.");
|
||
System.out.println("If you wish to QUIT the program please type 999.\n");
|
||
System.out.println("");
|
||
}
|
||
|
||
private static char determineLetterGrade(double average) {
|
||
// pretty self exlainatory:qw!
|
||
if (average >= 90)
|
||
return 'A';
|
||
if (average >= 80)
|
||
return 'B';
|
||
if (average >= 70)
|
||
return 'C';
|
||
if (average >= 60)
|
||
return 'D';
|
||
return 'F';
|
||
}
|
||
}
|