每个学生的属性包括学号、姓名、年龄等。每个学生要学习若干门课程,每门课程有平时成绩、考试成绩、实习成绩、期末考试成绩以及总评成绩等多个成绩,其中平时成绩的数目不确定(因为不同课程的教师布置方式不同),而总评成绩是其他成绩的平均值。请先设计合理的数据存放这些信息,然后设计程序完成以下功能: (1)列出某个学生的所有成绩; (2)列出某门课程每个学生总评成绩,并各个学生的总评成绩的总评分; (3)分区间统计某门课程的学生总评成绩,例如60分以下的学生人数,60至70分之间的学生人数等。用JAVA写
时间: 2024-01-01 09:06:23 浏览: 105
好的,根据您的需求,我可以给您提供以下Java代码实现:
```
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Student {
private String studentId;
private String name;
private int age;
private Map<String, ArrayList<Double>> courses;
public Student(String studentId, String name, int age, Map<String, ArrayList<Double>> courses) {
this.studentId = studentId;
this.name = name;
this.age = age;
this.courses = courses;
}
public String getStudentId() {
return studentId;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Map<String, ArrayList<Double>> getCourses() {
return courses;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setCourses(Map<String, ArrayList<Double>> courses) {
this.courses = courses;
}
public void addCourse(String courseName, ArrayList<Double> scores) {
courses.put(courseName, scores);
}
public void removeCourse(String courseName) {
courses.remove(courseName);
}
public ArrayList<Double> getCourseScores(String courseName) {
return courses.get(courseName);
}
public double getCourseAverage(String courseName) {
ArrayList<Double> scores = courses.get(courseName);
double sum = 0;
for (double score : scores) {
sum += score;
}
return sum / scores.size();
}
public double getTotalAverage() {
double sum = 0;
int count = 0;
for (Map.Entry<String, ArrayList<Double>> entry : courses.entrySet()) {
ArrayList<Double> scores = entry.getValue();
for (double score : scores) {
sum += score;
count++;
}
}
return sum / count;
}
public static void main(String[] args) {
ArrayList<Double> scores1 = new ArrayList<Double>();
scores1.add(80.0);
scores1.add(90.0);
scores1.add(85.0);
ArrayList<Double> scores2 = new ArrayList<Double>();
scores2.add(70.0);
scores2.add(85.0);
scores2.add(90.0);
ArrayList<Double> scores3 = new ArrayList<Double>();
scores3.add(90.0);
scores3.add(95.0);
scores3.add(80.0);
Map<String, ArrayList<Double>> coursesMap = new HashMap<String, ArrayList<Double>>();
coursesMap.put("math", scores1);
coursesMap.put("english", scores2);
coursesMap.put("history", scores3);
Student student = new Student("001", "Tom", 20, coursesMap);
// 列出某个学生的所有成绩
System.out.println("学生ID:" + student.getStudentId());
System.out.println("学生姓名:" + student.getName());
System.out.println("学生年龄:" + student.getAge());
for (Map.Entry<String, ArrayList<Double>> entry : student.getCourses().entrySet()) {
String courseName = entry.getKey();
ArrayList<Double> scores = entry.getValue();
System.out.println(courseName + "成绩:" + scores);
}
// 列出某门课程每个学生总评成绩,并各个学生的总评成绩的总评分
String courseName = "math";
System.out.println("课程名称:" + courseName);
double totalScore = 0;
for (Map.Entry<String, ArrayList<Double>> entry : student.getCourses().entrySet()) {
String studentName = entry.getKey();
ArrayList<Double> scores = entry.getValue();
double courseAverage = student.getCourseAverage(courseName);
double totalAverage = student.getTotalAverage();
double finalScore = (courseAverage + totalAverage) / 2;
System.out.println(studentName + "总评成绩:" + finalScore);
totalScore += finalScore;
}
System.out.println("总评分:" + totalScore);
// 分区间统计某门课程的学生总评成绩
int[] scoreRange = {60, 70, 80, 90, 100};
int[] count = new int[scoreRange.length + 1];
for (double score : student.getCourseScores(courseName)) {
int index = 0;
for (int i = 0; i < scoreRange.length; i++) {
if (score >= scoreRange[i]) {
index = i + 1;
} else {
break;
}
}
count[index]++;
}
System.out.println("60分以下的学生人数:" + count[0]);
for (int i = 0; i < scoreRange.length; i++) {
System.out.println(scoreRange[i] + "至" + scoreRange[i + 1] + "分之间的学生人数:" + count[i + 1]);
}
}
}
```
这段代码定义了一个`Student`类,包括学生ID、姓名、年龄和学生所学的课程信息,其中每门课程的成绩包括平时成绩、考试成绩、实习成绩、期末考试成绩以及总评成绩。在`main`方法中,我们可以根据需要调用类中的各种方法,来实现您所需要的功能。
阅读全文