建立一个学生类,要求完成以下功能:输出每个学生的姓名,学号,成绩;统计并输出学生的总人数,总成绩,平均成绩,最高成绩,最低成绩
时间: 2024-10-09 15:02:44 浏览: 25
在面向对象编程中,创建一个学生类是很常见的,我们可以按照以下步骤设计这个类:
```java
// 定义一个名为Student的类
public class Student {
// 定义属性,包括姓名、学号和成绩
private String name;
private int id;
private double score;
// 构造函数用于初始化学生信息
public Student(String name, int id, double score) {
this.name = name;
this.id = id;
this.score = score;
}
// 提供getter和setter方法访问和修改属性
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
// 输出学生信息的方法
@Override
public String toString() {
return "姓名:" + name + ", 学号:" + id + ", 成绩:" + score;
}
// 针对统计的功能,我们可以添加静态方法到Student类
public static void stats(Student[] students) {
if (students == null || students.length == 0) {
System.out.println("没有学生数据");
return;
}
int totalStudents = students.length;
double totalScore = 0.0, minScore = Double.MAX_VALUE, maxScore = Double.MIN_VALUE;
for (Student student : students) {
totalScore += student.getScore();
minScore = Math.min(minScore, student.getScore());
maxScore = Math.max(maxScore, student.getScore());
}
double averageScore = totalScore / totalStudents;
System.out.println("总人数:" + totalStudents);
System.out.println("总成绩:" + totalScore);
System.out.println("平均成绩:" + averageScore);
System.out.println("最高成绩:" + maxScore);
System.out.println("最低成绩:" + minScore);
}
}
```
阅读全文