用Java编写一个学生成绩管理系统。学生的属性包括学号、姓名、年龄等。每个学生要学习若干课程,每门课程有平时成绩、其中考试成绩、实习成绩、期末考试成绩以及总评成绩等多个成绩,其中平时成绩的数目不定(因为不同课程的教师布置的平时作业不同),而总评成绩是其它成绩的平均值。请先设计合理的类存放这些信息,然后设计程序完成以下功能: (1)列出某个学生的所有成绩; (2)列出某门课程每个学生的总评成绩,及所有学生总评成绩的总评分; (3)分区段统计某门课程的学生总评成绩,例如60分以下的学生人数、60至70分的学生人数等。
时间: 2024-02-09 15:09:59 浏览: 130
好的,下面是我为您提供的Java代码:
```
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentGradeManagementSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
List<Student> students = new ArrayList<>(); // 存储学生信息
List<Course> courses = new ArrayList<>(); // 存储课程信息
// 添加学生信息
System.out.print("请输入学生数:");
int numStudents = input.nextInt();
for (int i = 0; i < numStudents; i++) {
System.out.print("请输入第" + (i+1) + "个学生的信息:");
System.out.print("学号:");
String id = input.next();
System.out.print("姓名:");
String name = input.next();
System.out.print("年龄:");
int age = input.nextInt();
students.add(new Student(id, name, age));
}
// 添加课程信息
System.out.print("请输入课程数:");
int numCourses = input.nextInt();
for (int i = 0; i < numCourses; i++) {
System.out.print("请输入第" + (i+1) + "门课程的信息:");
System.out.print("课程名称:");
String name = input.next();
System.out.print("平时成绩占比(例如0.3表示30%):");
double dailyScoreWeight = input.nextDouble();
System.out.print("其中考试成绩占比:");
double examScoreWeight = input.nextDouble();
System.out.print("实习成绩占比:");
double practiceScoreWeight = input.nextDouble();
System.out.print("期末考试成绩占比:");
double finalScoreWeight = input.nextDouble();
courses.add(new Course(name, dailyScoreWeight, examScoreWeight, practiceScoreWeight, finalScoreWeight));
}
// 输入每个学生的课程成绩
for (Student student : students) {
System.out.print("请输入学生" + student.getName() + "的各科成绩:");
for (Course course : courses) {
System.out.print(course.getName() + "的平时成绩数目:");
int numDailyScore = input.nextInt();
List<Double> dailyScores = new ArrayList<>();
for (int i = 0; i < numDailyScore; i++) {
System.out.print("第" + (i+1) + "个平时成绩:");
dailyScores.add(input.nextDouble());
}
System.out.print(course.getName() + "的其中考试成绩:");
double examScore = input.nextDouble();
System.out.print(course.getName() + "的实习成绩:");
double practiceScore = input.nextDouble();
System.out.print(course.getName() + "的期末考试成绩:");
double finalScore = input.nextDouble();
student.addCourseScore(course, dailyScores, examScore, practiceScore, finalScore);
}
}
// 列出某个学生的所有成绩
System.out.print("请输入要查询成绩的学生姓名:");
String name = input.next();
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println(student);
}
}
// 列出某门课程每个学生的总评成绩,及所有学生总评成绩的总评分
System.out.print("请输入要查询总评成绩的课程名称:");
String courseName = input.next();
Course course = null;
for (Course c : courses) {
if (c.getName().equals(courseName)) {
course = c;
break;
}
}
if (course == null) {
System.out.println("未找到该课程!");
return;
}
double totalScore = 0;
int numStudentsWithScore = 0;
for (Student student : students) {
double score = student.getScore(course);
if (score != -1) {
System.out.println(student.getName() + "的" + course.getName() + "总评成绩为:" + score);
totalScore += score;
numStudentsWithScore++;
}
}
double averageScore = totalScore / numStudentsWithScore;
System.out.println("所有学生的" + course.getName() + "总评成绩的总评分为:" + averageScore);
// 分区段统计某门课程的学生总评成绩
System.out.print("请输入要查询的课程名称:");
courseName = input.next();
course = null;
for (Course c : courses) {
if (c.getName().equals(courseName)) {
course = c;
break;
}
}
if (course == null) {
System.out.println("未找到该课程!");
return;
}
System.out.print("请输入分数段的个数:");
int numSections = input.nextInt();
double[] sectionScores = new double[numSections+1];
sectionScores[0] = 0; // 分数段的下限
sectionScores[numSections] = 100; // 分数段的上限
for (int i = 1; i < numSections; i++) {
System.out.print("请输入第" + i + "个分数段的下限:");
sectionScores[i] = input.nextDouble();
}
int[] numStudentsInSection = new int[numSections+1];
for (Student student : students) {
double score = student.getScore(course);
if (score != -1) {
for (int i = 0; i < numSections; i++) {
if (score >= sectionScores[i] && score < sectionScores[i+1]) {
numStudentsInSection[i]++;
break;
}
}
}
}
for (int i = 0; i < numSections; i++) {
System.out.println(sectionScores[i] + "到" + sectionScores[i+1] + "分的学生人数为:" + numStudentsInSection[i]);
}
}
}
class Student {
private String id;
private String name;
private int age;
private List<CourseScore> courseScores;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
this.courseScores = new ArrayList<>();
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void addCourseScore(Course course, List<Double> dailyScores, double examScore, double practiceScore, double finalScore) {
this.courseScores.add(new CourseScore(course, dailyScores, examScore, practiceScore, finalScore));
}
public double getScore(Course course) {
for (CourseScore courseScore : courseScores) {
if (courseScore.getCourse().equals(course)) {
return courseScore.getScore();
}
}
return -1;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (CourseScore courseScore : courseScores) {
sb.append(courseScore.getCourse().getName()).append("成绩:").append(courseScore.getScore()).append("\n");
}
return sb.toString();
}
}
class Course {
private String name;
private double dailyScoreWeight;
private double examScoreWeight;
private double practiceScoreWeight;
private double finalScoreWeight;
public Course(String name, double dailyScoreWeight, double examScoreWeight, double practiceScoreWeight, double finalScoreWeight) {
this.name = name;
this.dailyScoreWeight = dailyScoreWeight;
this.examScoreWeight = examScoreWeight;
this.practiceScoreWeight = practiceScoreWeight;
this.finalScoreWeight = finalScoreWeight;
}
public String getName() {
return name;
}
public double getDailyScoreWeight() {
return dailyScoreWeight;
}
public double getExamScoreWeight() {
return examScoreWeight;
}
public double getPracticeScoreWeight() {
return practiceScoreWeight;
}
public double getFinalScoreWeight() {
return finalScoreWeight;
}
}
class CourseScore {
private Course course;
private List<Double> dailyScores;
private double examScore;
private double practiceScore;
private double finalScore;
public CourseScore(Course course, List<Double> dailyScores, double examScore, double practiceScore, double finalScore) {
this.course = course;
this.dailyScores = dailyScores;
this.examScore = examScore;
this.practiceScore = practiceScore;
this.finalScore = finalScore;
}
public Course getCourse() {
return course;
}
public double getScore() {
double dailyScoreSum = 0;
for (double dailyScore : dailyScores) {
dailyScoreSum += dailyScore;
}
double dailyScoreAverage = dailyScoreSum / dailyScores.size();
return dailyScoreAverage * course.getDailyScoreWeight() +
examScore * course.getExamScoreWeight() +
practiceScore * course.getPracticeScoreWeight() +
finalScore * course.getFinalScoreWeight();
}
}
```
以上代码实现了一个学生成绩管理系统,包括学生信息、课程信息和课程成绩信息。每个学生可以学习多门课程,每门课程有多个成绩,包括平时成绩、其中考试成绩、实习成绩、期末考试成绩以及总评成绩等。可以实现以下功能:
1. 列出某个学生的所有成绩;
2. 列出某门课程每个学生的总评成绩,及所有学生总评成绩的总评分;
3. 分区段统计某门课程的学生总评成绩。
阅读全文