题目:
学生成绩管理:设计一个学生成绩管理系统,使用ArrayList或LinkedList存储学生信息。每个学生有一个唯一的学号、姓名和成绩,实现添加、删除、查找和按照成绩进行排序等功能。
代码:
package com.example.practice;
//学生成绩管理:设计一个学生成绩管理系统,使用ArrayList或LinkedList存储学生信息。
// 每个学生有一个唯一的学号、姓名和成绩,实现添加、删除、查找和按照成绩进行排序等功能。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
class Student {
private int id;
private String name;
private int score;
public Student(int id, String name, int score) {
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
public class Practice15 {
private List<Student> students;
public Practice15() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(int id) {
for (Student student : students) {
if (student.getId() == id) {
students.remove(student);
break;
}
}
}
public Student findStudent(int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
public void sortStudentsByScore() {
Collections.sort(students, Comparator.comparingInt(Student::getScore));
}
public void displayStudents() {
for (Student student : students) {
System.out.println("Student ID: " + student.getId());
System.out.println("Name: " + student.getName());
System.out.println("Score: " + student.getScore());
System.out.println("--------------------------");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Practice15 managementSystem = new Practice15();
boolean running = true;
while (running) {
System.out.println("1. Add a student");
System.out.println("2. Remove a student");
System.out.println("3. Find a student");
System.out.println("4. Sort students by score");
System.out.println("5. Display all students");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter student ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter student name: ");
String name = scanner.nextLine();
System.out.print("Enter student score: ");
int score = scanner.nextInt();
scanner.nextLine();
Student student = new Student(id, name, score);
managementSystem.addStudent(student);
System.out.println("Student added successfully!");
break;
case 2:
System.out.print("Enter student ID: ");
int removeId = scanner.nextInt();
scanner.nextLine();
managementSystem.removeStudent(removeId);
System.out.println("Student removed successfully!");
break;
case 3:
System.out.print("Enter student ID: ");
int findId = scanner.nextInt();
scanner.nextLine();
Student foundStudent = managementSystem.findStudent(findId);
if (foundStudent != null) {
System.out.println("Student ID: " + foundStudent.getId());
System.out.println("Name: " + foundStudent.getName());
System.out.println("Score: " + foundStudent.getScore());
} else {
System.out.println("Student not found!");
}
break;
case 4:
managementSystem.sortStudentsByScore();
System.out.println("Students sorted by score!");
break;
case 5:
managementSystem.displayStudents();
break;
case 0:
running = false;
break;
default:
System.out.println("Invalid choice!");
}
}
scanner.close();
}
}
运行结果截图: