I'm writing a program that reads students scores, gets the best score, and then assigns grades based on
Grade is A if score is >= best - 10;
Grade is B if score is >= best - 20;
Grade is C if score is >= best - 30;
Grade is D if score is >= best - 40;
Grade is F otherwise;
Here's the 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
__________
here's what I have so far:
import java.util.Scanner;
public class assigningGrades {
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Get number of students
System.out.print("Enter number of students: ");
int numberOfStudents = input.nextInt();
int[] scores = new int[numberOfStudents]; // Array scores
int best = 0; // The best score
char grade; // The grade
// Read scores and find the best score
System.out.print("Enter " + numberOfStudents + " scores: ");
for (int i = 0; i < scores.length; i++) {
if ( scores[i] > best)
best = scores[i];
//find the highest score
}
// Declare and initialize output string
String output = "";
// Assign and display grades
for (int i = 0; i < scores.length; i++) {
if (scores[i] >= best - 10)
grade = 'A';
else if (scores[i] >= best - 20)
grade = 'B';
else if (scores[i] >= best - 30)
grade = 'C';
else if (scores[i] >= best - 40)
grade = 'D';
else
grade = 'F';
output += "Student " + i + " score is " +
scores[i] + " and grade is " + grade + "\n";
}
// Display the result
System.out.println(output);
}
}

New Topic/Question
Reply




MultiQuote




|