Can someone please tell me why my code is not providing the correct output? Here aare the instructions "

I need to write a program that reads student scores, gets the best score, and then assigns grades based on the following scheme:

1) Grade is A if score is >= best - 10

2) Grade is B if score is >= best - 20;

3) Grade is C if score is >= best - 30;

4) Grade is D if score is >= best - 40;

5) 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. My problem comes from pulling the grades from an array, this is what I have so far:

// Here is my code. Thank You

import java.util.Scanner; // imports the scanner function


public class NBpractice { //class is formed
    public static void main(String []args) { // main method


        // user input is asked for the number of students
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of students: "); 
        int studentNum = input.nextInt();

        //user input is asked for students scores
        Scanner input2 = new Scanner(System.in);
        System.out.print("Enter " + studentNum + " scores: ");
        int scores = input2.nextInt();


        int best = 80;
        char letterGrade;




int scoresArray[] = new int[studentNum]; // array is created and holds the # of place values as students

for (int i = 0; i < scoresArray.length; i++) { // for loop created

scoresArray[i] = input2.nextInt(); //array values are assigned to user's input



            best = scoresArray[0];
            if (best < scoresArray[i]) {

                best = scoresArray[i];
            }

            //-----------------------------------------------------------------------------
            if (scores >= (best - 10)) {

                letterGrade = 'A';
            }

            else if (scores >= (best - 20)) {
                letterGrade = 'B';
            }

            else if (scores

                    >= (best - 30)) {
                letterGrade = 'C';
            }

            else if (scores >= (best - 30)) {
                letterGrade = 'D';
            }

            else {
                letterGrade = 'F';
            }


            System.out.println("Student " + i + " Score is " + scoresArray[i] + " and grade is: " + letterGrade ); 

        }


        //------------------------------------------------------------

        }

    }

3 Answers

1
npinti On

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.

0
Hashim Kayani On

You'll 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]
0
Stephan Hogenboom On

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);
    }
  }