
本教程详细阐述了如何在java中处理包含字符串和整数变量的对象数组,以计算其中特定整数属性(如学生分数)的平均值和最大值。文章将通过一个学生管理示例,演示如何正确设计对象类、遍历对象数组、提取数值数据,并高效地进行统计计算。
在Java应用程序开发中,我们经常需要对自定义对象数组中的数据进行统计分析,例如计算某个数值属性的平均值或找出最大值。本教程将以一个“学生分数统计”的场景为例,详细指导如何实现这一功能。
首先,我们需要定义一个 Student 类来表示学生信息,该类包含学生的姓名(name,字符串类型)和分数(score,整数类型)。为了遵循良好的面向对象设计原则,我们将为这些属性提供构造函数、设置器(setters)和获取器(getters)。特别需要注意的是,获取器 getScore() 应该只返回分数,而不接受任何参数。
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 String getName() {
return this.name;
}
// 获取学生分数
public int getScore() {
return this.score;
}
// 从控制台读取学生输入
public void readInput() {
Scanner keyboard = new Scanner(System.in);
System.out.print("请输入学生姓名: ");
this.name = keyboard.next();
System.out.print("请输入学生分数: ");
this.score = keyboard.nextInt();
}
// 输出学生信息
public void writeOutput() {
System.out.println("学生姓名: " + name + ", 分数: " + score + "%");
}
}注意事项:
接下来,我们创建 TestReporter 类,用于管理学生数组并计算统计数据。该类将包含一个 Student 对象数组、学生总数以及计算出的最高分和平均分。
立即学习“Java免费学习笔记(深入)”;
import 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.print("请输入学生总数: ");
numOfStudents = keyboard.nextInt();
ourClass = new Student[numOfStudents];
for (int i = 0; i < numOfStudents; i++) {
System.out.println("\n正在输入第 " + (i + 1) + " 位学生信息:");
ourClass[i] = new Student();
ourClass[i].readInput();
}
keyboard.close(); // 关闭Scanner以释放资源
}
// 计算统计数据:平均分和最高分
public void computeStats() {
// 检查数组是否为空,避免空指针异常和除以零错误
if (ourClass == null || numOfStudents == 0) {
System.out.println("没有学生数据可用于计算统计。");
this.highestScore = 0;
this.averageScore = 0.0;
return;
}
double totalScore = 0;
// 初始化最高分:假设至少有一个学生,将其分数作为初始最高分
// 更健壮的做法是初始化为Integer.MIN_VALUE
this.highestScore = ourClass[0].getScore();
for (int i = 0; i < numOfStudents; i++) {
int currentScore = ourClass[i].getScore(); // 获取当前学生的分数
totalScore += currentScore; // 累加总分
if (currentScore > this.highestScore) {
this.highestScore = currentScore; // 更新最高分
}
}
this.averageScore = totalScore / numOfStudents; // 计算平均分
}
// 显示学生信息及统计结果
public void displayResults() {
// 在显示结果前确保统计数据已计算
computeStats();
System.out.println("\n--- 学生分数报告 ---");
for (Student student : ourClass) {
student.writeOutput();
}
System.out.println("\n--- 统计摘要 ---");
System.out.printf("平均分数: %.2f%n", this.averageScore);
System.out.println("最高分数: " + this.highestScore);
System.out.println("--------------------");
}
// 主方法,用于测试
public static void main(String[] args) {
TestReporter reporter = new TestReporter();
reporter.getData(); // 获取学生数据
reporter.displayResults(); // 计算并显示结果
}
}在 computeStats() 方法中,我们执行了以下关键步骤:
以上就是Java中计算对象数组特定属性的平均值与最大值教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号