计算包含字符串和整数变量的对象数组的平均值

本文将详细介绍如何计算一个对象数组的平均值,其中对象包含字符串和整数变量。我们将以一个学生类为例,该类包含姓名(字符串)和分数(整数)。通过示例代码,我们将演示如何遍历数组,提取分数,计算总和,并最终计算出平均分。同时,我们还会展示如何找到数组中的最高分。

1. 定义 Student 类

首先,我们需要定义一个 Student 类,该类包含学生的姓名和分数两个属性。

import java.util.Scanner;

public class Student {

    private String name;
    private int score;

    public Student() {
    }

    public Student(String name, int score){
        this.name = name;
        this.score = score;

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

    public void readInput() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter the student's name: ");
        this.name = keyboard.next();
        System.out.println("Please enter the student's score: ");
        this.score = keyboard.nextInt();
    }
    public void writeOutput() {
        System.out.println("The student's name and score: " + name + ", " + score + "%");
    }

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

注意:

  • getName() 和 getScore() 方法用于获取私有变量的值。
  • setName() 和 setScore() 方法用于设置私有变量的值。虽然在此示例中未使用setName(),但通常提供它们以允许修改对象的状态。
  • readInput()方法用于从控制台读取学生姓名和成绩。
  • writeOutput()方法用于将学生姓名和成绩输出到控制台。

2. 定义 TestReporter 类

接下来,我们需要定义一个 TestReporter 类,该类负责管理学生数组,计算平均分和最高分,并显示结果。

impor

t java.util.Scanner; public class TestReporter { private int highestScore; private double averageScore; private Student[] ourClass; private int numOfStudents; public TestReporter(){ } public void getData() { Scanner keyboard = new Scanner(System.in); System.out.println("Please enter the number of students"); numOfStudents = keyboard.nextInt(); ourClass = new Student[numOfStudents]; for (int i = 0; i < numOfStudents ; i++) { ourClass[i] = new Student(); ourClass[i].readInput(); } } public void computeStats() { double total = 0; highestScore = 0; // 初始化最高分 for (int i = 0; i < numOfStudents; i++) { int score = ourClass[i].getScore(); total += score; // 累加分数 if (score > highestScore) { highestScore = score; // 更新最高分 } } averageScore = total / ourClass.length; } public void displayResults() { computeStats(); // 先计算统计数据 for (Student student: ourClass) { student.writeOutput(); } System.out.println("Average Score = " + averageScore); System.out.println("Highest Score = " + highestScore); } public static void main(String[] args) { TestReporter reporter = new TestReporter(); reporter.getData(); reporter.displayResults(); } }

代码解释:

  • getData() 方法:从用户输入获取学生人数,并创建相应大小的 Student 数组。然后,循环遍历数组,为每个学生对象调用 readInput() 方法来读取学生信息。
  • computeStats() 方法:
    • 初始化 total 变量为 0,用于累加所有学生的分数。
    • 初始化 highestScore 变量为 0,用于记录最高分。
    • 循环遍历 ourClass 数组,获取每个学生的分数。
    • 将分数累加到 total 变量中。
    • 如果当前学生的分数大于 highestScore,则更新 highestScore。
    • 计算平均分 averageScore,通过总分除以学生人数。
  • displayResults() 方法:
    • 先调用 computeStats() 方法计算平均分和最高分。
    • 循环遍历 ourClass 数组,调用每个学生对象的 writeOutput() 方法来显示学生信息。
    • 打印平均分和最高分。
  • main() 方法:创建 TestReporter 类的实例,调用 getData() 方法获取数据,然后调用 displayResults() 方法显示结果。

3. 运行程序

编译并运行 TestReporter 类,程序会提示您输入学生人数,然后依次输入每个学生的姓名和分数。最后,程序会显示每个学生的信息,以及平均分和最高分。

注意事项

  • 确保 Student 类中的 getScore() 方法返回的是 int 类型。
  • 在 computeStats() 方法中,需要初始化 highestScore 变量。
  • 在 displayResults() 方法中,需要先调用 computeStats() 方法,再显示结果。
  • 代码中使用了 Scanner 类从控制台读取输入,需要导入 java.util.Scanner 包。
  • 为了代码的健壮性,可以添加输入验证,例如确保分数在合理范围内。

总结

通过以上步骤,我们成功地计算了包含字符串和整数变量的对象数组的平均值和最高值。这个例子展示了如何遍历对象数组,访问对象的属性,并进行相应的计算。 这个方法可以推广到其他类型的对象数组,只需要修改对象类的定义和计算逻辑即可。