In: Computer Science
In Java, I need a program that will ask a user to input how many tests they want the average of. For example, It needs to ask the user how many tests would you like the average of? When the user enters the amount of tests they want the average score of, the program needs to give them that many test scores to input. Then with the average, determine what their grade is, if 90-100=A if 80-90 = B etc. Java and please explain.
import java.util.Scanner;
public class AverageGrades {
// Function to get letter grade by passing average as input parameter
public static String getLetterGrade(double average) {
if (average < 60) {
return "F";
} else if (average < 75) {
return "C";
} else if (average < 80) {
return "B";
} else {
return "A";
}
}
public static void main(String[] args) {
int n;
Scanner scanner = new Scanner(System.in);
// reading value for n
System.out.print("How many tests? ");
n = scanner.nextInt();
// If the value of n is negative then reading it again
while(n<0){
System.out.println("Invalid input");
System.out.print("How many tests? ");
n = scanner.nextInt();
}
double sum = 0, grade;
//Reading n values from user and calculating sum of grades
System.out.println("Enter "+n+" scores");
for(int i = 0;i<n;i++){
grade = scanner.nextDouble();
sum += grade;
}
// Calculating average
double average = (sum/n);
// Printing average
System.out.println("Average score = "+(average));
// Printing letter grade
System.out.println("Letter grade = "+getLetterGrade(average));
}
}


