编写程序以读取学生分数,获得最高分数,然后分配分数

时间:2019-05-12 22:19:43

标签: java arrays loops java.util.scanner

有人可以告诉我为什么我的代码没有提供正确的输出吗?这里是说明“

  

我需要编写一个程序来读取学生的分数,并获得最佳成绩   得分,然后根据以下方案分配成绩:

     

1)如果分数> =最佳-10

,则等级为A。      

2)如果分数> =最佳-20,则成绩为B;

     

3)如果分数> =最佳-30,则成绩为C;

     

4)如果分数> =最佳-40,则成绩为D;

     

5)否则成绩为F。

该程序提示用户输入学生总数,然后提示用户输入所有分数,并通过显示成绩得出结论。我的问题来自从数组中获得成绩,这就是我到目前为止所掌握的:

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

        }


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

        }

    }

4 个答案:

答案 0 :(得分:1)

一些指针...

  • 这:System.out.print("Enter " + studentNum + " scores: ");int scores = input2.nextInt();需要进入for循环主体。
  • 使用for循环填充数组。
  • 执行for循环后,在数组中找到best(最高)分数。
  • 使用另一个for循环对成绩进行排序。

按原样,您的程序只会要求成绩,并且几乎都假设最佳成绩为80,但情况并非总是如此。

答案 1 :(得分:0)

您将需要两个单独的for循环。一种是阅读成绩并获得最佳成绩,另一种是for循环以标准化成绩。

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

测试输入80 60 75 83 67的输出:

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

答案 2 :(得分:0)

我建议您使用学生班级,而不适合parallel lists or arrays使用。例如,一个学生班级可能看起来像这样:

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 + '\'' + '}';
  }
}

然后您可以创建学生实例,并将其添加到public static void main中的ArrayList中。

我认为您必须使用两个循环,因为您无法事先知道最佳成绩。在您的主菜单中,可以输入学生,将他们添加到List并比较他们的成绩;

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

答案 3 :(得分:0)

第7章软件包

导入java.util.Scanner;

公共课程Exercise7_1 {

public static void main(String[] args) {
    // Assign grades

    Scanner input = new Scanner(System.in);
    int numStudents;
    int[] scores;
    int best;

    System.out.println("Enter the number of students:  ");
    numStudents = input.nextInt();
    scores = new int[numStudents];

    System.out.println("Enter " + numStudents + " scores:  ");
    for (int i = 0; i < numStudents; i++) {
        scores[i] = input.nextInt();
    }

    displayGrades(findBestScore(scores), scores);


}

public static int findBestScore(int[] scores) {
    int best = scores[0];
    for (int i = 1; i < scores.length-1; i++) {
        if (scores[i] > best) 
            best = scores[i];
    }
    return best;
}

public static void displayGrades(int best, int[] scores ) {

    char grade = ' ';
    for (int i = 0; i < scores.length; i++) {
        if (scores[i] >= best-10)
            grade = 'A';
        else if (best - 10 > scores[i] && scores[i] >= best - 20)
            grade = 'B';
        else if (best - 20 > scores[i] && scores[i] >= best -30)
            grade = 'C';
        else if (best - 30 > scores[i] && scores[i] >= best -40)
            grade = 'D';
        else if (best - 40 > scores[i])
            grade = 'F';

        System.out.println("Student " + i + " score is " + scores[i] + " and grade is " + grade);
    }
}

}