java – Write a program that reads student scores, gets the best score, and then assigns grades

java – Write a program that reads student scores, gets the best score, and then assigns grades

Some pointers…

  • This: System.out.print(Enter + studentNum + scores: ); and int scores = input2.nextInt(); need to go in the for loop body.
  • Use the for loop to populate the array.
  • Once that the for loop is executed, find the best (highest) score in the array.
  • Use another for loop to sort out the grades.

As is, your program will only ask for the grades and pretty much assumes that the best grade is 80, which might not always be the case.

Youll need two separate for loops. One to read the grades, and get the best, and the second for loop to normalize the grades.

int[] scores = new int[amount];
int best = -1;
for(int i = 0; i < amount; i++)
{
    scores[i] = in.nextInt();

    if(scores[i] > best)
        best = scores[i];
}
System.out.println(Arrays.toString(scores));

// Now that we have the best, we can normalize
// the rest of the scores based on the best
// and assign the corresponding letter grade.
String[] grades = new String[amount];
for(int i = 0; i < amount; i++)
{
    int score = scores[i] * 100 / best;

    if(score >= 90)
        grades[i] = A;
    else if(score >= 80)
        grades[i] = B;
    else if(score >= 70)
        grades[i] = C;
    else if(score >= 60)
        grades[i] = D;
    else
        grades[i] = F;

    scores[i] = score;
}
System.out.println(Arrays.toString(scores));
System.out.println(Arrays.toString(grades));

Test input 80 60 75 83 67 outputs:

[80, 60, 75, 83, 67]
[96, 72, 90, 100, 80]
[A, C, A, A, B]

java – Write a program that reads student scores, gets the best score, and then assigns grades

I would recommend using a Student class and not work with parallel lists or arrays. A Student class can for example look like this:

class Student {
  int score;
  String grade; // could also be an Enum

  public int getScore() {
    return this.score;
  }

  public void setScore(int score) {
    this.score = score;
  }

  public String getGrade() {
    return this.grade;
  }

  public void setGrade(String grade) {
    this.grade = grade;
  }

  @Override
  public String toString() {
    return Student{ + score= + score + , grade= + grade +  + };
  }
}

You can then make instance of the Students and add them to an ArrayList in you public static void main.

I think you have to use two loops because you cannot know beforehand what the best grade will be. In your main you can make enter the students add them to a List and compare their grades;

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print(Enter the number of students: );


    int highScore = 0;
    int numOfStudent = input.nextInt();
    List<Student> studentList = new ArrayList<>();

    for (int i = 1; i <= numOfStudent; i++) { // you might want to add Exception handling here, by surrounding it with a try / catch or do more checks than only i <= numOfStudent
      System.out.printf(please fill in the score of student no %d n, i);
      int score = input.nextInt();
      Student student = new Student();
      student.setScore(score);


      if (score > highScore) {
        highScore = score;
      }
      studentList.add(student);
    }

    System.out.println(these are the scores and grades of the Students);

    for (Student s : studentList) {
      if (s.getScore() >= highScore - 10) {
        s.setGrade(A);
      }
      else if (s.getScore() >= highScore - 20) {
        s.setGrade(B);
      }
      else if (s.getScore() >= highScore - 30) {
        s.setGrade(C);
      }
      else if (s.getScore() >= highScore - 40) {
        s.setGrade(D);
      }
      else {
        s.setGrade(F);
      }
      System.out.println(s);
    }
  }

Leave a Reply

Your email address will not be published.