用java编写一个学生管理程序,选择集合类存储学生的学号、姓名和期末考试各科的成绩(课程名+成绩,假设只包括数学、语文和英语三门课程)。实现如下功能: (1)学生信息(学号+姓名)录入; (2)学生成绩录入(课程+得分); (3)根据姓名查找学号。 (4)根据学号查找姓名。 (5)根据学号查找成绩。 (6)根据学号和课程名查找学生的成绩。 (7)根据学号计算平均分,并屏幕输出。 (8)根据平均分对班级学生成绩排序,按从高到低的顺序输出姓名和平均成绩。注意用arraylist存储学生信息
时间: 2023-12-16 07:06:13 浏览: 64
好的,以下是Java代码实现:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class StudentManagementSystem {
static ArrayList<Student> students = new ArrayList<Student>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1、学生信息录入");
System.out.println("2、学生成绩录入");
System.out.println("3、根据姓名查找学号");
System.out.println("4、根据学号查找姓名");
System.out.println("5、根据学号查找成绩");
System.out.println("6、根据学号和课程名查找学生的成绩");
System.out.println("7、根据学号计算平均分");
System.out.println("8、根据平均分对班级学生成绩排序");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent(scanner);
break;
case 2:
addScore(scanner);
break;
case 3:
searchIdByName(scanner);
break;
case 4:
searchNameById(scanner);
break;
case 5:
searchScoreById(scanner);
break;
case 6:
searchScoreByIdAndCourse(scanner);
break;
case 7:
calculateAverageScoreById(scanner);
break;
case 8:
sortStudentsByAverageScore();
break;
default:
System.out.println("输入不合法,请重新输入!");
}
}
}
// 添加学生信息
public static void addStudent(Scanner scanner) {
System.out.println("请输入学号:");
int id = scanner.nextInt();
System.out.println("请输入姓名:");
String name = scanner.next();
students.add(new Student(id, name));
System.out.println("学生信息添加成功!");
}
// 添加学生成绩
public static void addScore(Scanner scanner) {
System.out.println("请输入学号:");
int id = scanner.nextInt();
System.out.println("请输入课程名(数学、语文、英语):");
String course = scanner.next();
System.out.println("请输入成绩:");
int score = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
student.getScores().put(course, score);
System.out.println("成绩添加成功!");
return;
}
}
System.out.println("学号不存在!");
}
// 根据姓名查找学号
public static void searchIdByName(Scanner scanner) {
System.out.println("请输入姓名:");
String name = scanner.next();
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println("学号为:" + student.getId());
return;
}
}
System.out.println("姓名不存在!");
}
// 根据学号查找姓名
public static void searchNameById(Scanner scanner) {
System.out.println("请输入学号:");
int id = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
System.out.println("姓名为:" + student.getName());
return;
}
}
System.out.println("学号不存在!");
}
// 根据学号查找成绩
public static void searchScoreById(Scanner scanner) {
System.out.println("请输入学号:");
int id = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
System.out.println(student.getScores());
return;
}
}
System.out.println("学号不存在!");
}
// 根据学号和课程名查找学生的成绩
public static void searchScoreByIdAndCourse(Scanner scanner) {
System.out.println("请输入学号:");
int id = scanner.nextInt();
System.out.println("请输入课程名(数学、语文、英语):");
String course = scanner.next();
for (Student student : students) {
if (student.getId() == id) {
System.out.println(course + "成绩为:" + student.getScores().get(course));
return;
}
}
System.out.println("学号不存在!");
}
// 根据学号计算平均分
public static void calculateAverageScoreById(Scanner scanner) {
System.out.println("请输入学号:");
int id = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
double sum = 0;
int count = 0;
for (int score : student.getScores().values()) {
sum += score;
count++;
}
double averageScore = sum / count;
System.out.println("平均分为:" + averageScore);
return;
}
}
System.out.println("学号不存在!");
}
// 根据平均分对班级学生成绩排序
public static void sortStudentsByAverageScore() {
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
double s1AverageScore = s1.getAverageScore();
double s2AverageScore = s2.getAverageScore();
if (s1AverageScore > s2AverageScore) {
return -1;
} else if (s1AverageScore < s2AverageScore) {
return 1;
}
return 0;
}
});
System.out.println("按平均分排序的学生名单:");
for (Student student : students) {
System.out.println(student.getName() + " " + student.getAverageScore());
}
}
}
class Student {
private int id;
private String name;
private ArrayList<String> courses = new ArrayList<String>() {{
add("数学");
add("语文");
add("英语");
}};
private ArrayList<Integer> scores = new ArrayList<Integer>() {{
add(0);
add(0);
add(0);
}};
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public ArrayList<String> getCourses() {
return courses;
}
public ArrayList<Integer> getScores() {
return scores;
}
public double getAverageScore() {
double sum = 0;
int count = 0;
for (int score : scores) {
sum += score;
count++;
}
return sum / count;
}
}
```
注意,这里使用了ArrayList来存储学生信息和成绩,每个学生的成绩使用了一个HashMap来存储。在根据平均分排序时,我们使用了Collections.sort()方法,并传入一个Comparator来指定排序方式。
阅读全文