In: Computer Science
Sections 7.1
Write a program that reads student scores, gets the best score, and the assigns grades based on the following scheme:
Grade is A if scores is >= best - 10;
Grade is B if scores is >= best - 20;
Grade is C if scores is >= best - 30;
Grade is D if scores is >= best - 40;
Grade is F otherwise.
The program prompts the user to enter the total number of students, then prompts the user to enter all of the scores, and concludes by displaying the grades. Here is a sample run:
Sample run:
Enter the number of students: 4
Enter 4 scores: 40 55 70 58
Student 0 score is 40 and grade is C
Student 1 score is 55 and grade is B
Student 2 score is 70 and grade is A
Student 3 score is 58 and grade is B
Please write in Java Script.
Thank you!
import java.util.Scanner;
public class StudentGrades {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int n= in.nextInt();
System.out.print("Enter " + n + " scores: ");
int[] scores = new int[n];
int best = 0, score;
char grade;
for (int i =0 ; i < n; i++) {
scores[i] = in.nextInt();
if (scores[i] > best)
best = scores[i];
}
for (int i =0 ; i < n; i++) {
score = scores[i];
if (score >= best - 10) {
grade = 'A';
} else if (score >= best - 20) {
grade = 'B';
} else if (score >= best - 30) {
grade = 'C';
} else if (score >= best - 40) {
grade = 'D';
} else {
grade = 'F';
}
System.out.printf("Student %d score is %d and grade is %c\n", i, score, grade);
}
}
}
