编写一个Java程序,实现一个具有学生成绩管理系统的单链表 1)设计单链表 2) 能够完成学生成绩管理系统简单功能。 3) 学生属性包括:姓名,学号,班级,性别。 4) 学生成绩包括:语文,数学,英语,计算机基础,数据结构。 5) 成绩管理系统管理功能: a)能够输出显示任意一个学生的各科成绩,平均成绩,不及格成绩列表。 b) 能够计算全班各科平均成绩,各科不及格学生名单列表。 c)全班学生成绩总表。
时间: 2024-02-14 19:32:47 浏览: 82
以下是一个简单的Java程序,实现了一个具有学生成绩管理系统的单链表,包含上述要求的功能:
```java
import java.util.Scanner;
class Student {
String name;
int id;
String className;
String gender;
int chinese;
int math;
int english;
int computer;
int dataStructure;
public Student(String name, int id, String className, String gender, int chinese, int math, int english, int computer, int dataStructure) {
this.name = name;
this.id = id;
this.className = className;
this.gender = gender;
this.chinese = chinese;
this.math = math;
this.english = english;
this.computer = computer;
this.dataStructure = dataStructure;
}
public void display() {
System.out.println("Name: " + name + "\nID: " + id + "\nClass: " + className + "\nGender: " + gender);
System.out.println("Chinese: " + chinese + "\nMath: " + math + "\nEnglish: " + english + "\nComputer: " + computer + "\nData Structure: " + dataStructure);
}
public int totalScore() {
return chinese + math + english + computer + dataStructure;
}
public double averageScore() {
return (double) totalScore() / 5;
}
public boolean isFailed() {
return chinese < 60 || math < 60 || english < 60 || computer < 60 || dataStructure < 60;
}
}
class Node {
Student student;
Node next;
public Node(Student student) {
this.student = student;
this.next = null;
}
}
class LinkedList {
Node head;
public LinkedList() {
head = null;
}
public void add(Student student) {
Node node = new Node(student);
if (head == null) {
head = node;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
}
}
public void display() {
Node temp = head;
while (temp != null) {
temp.student.display();
temp = temp.next;
}
}
public void displayStudentById(int id) {
Node temp = head;
while (temp != null) {
if (temp.student.id == id) {
temp.student.display();
break;
}
temp = temp.next;
}
}
public void displayFailedStudents() {
Node temp = head;
while (temp != null) {
if (temp.student.isFailed()) {
System.out.println(temp.student.name);
}
temp = temp.next;
}
}
public void displayAverageScore() {
int chineseTotal = 0;
int mathTotal = 0;
int englishTotal = 0;
int computerTotal = 0;
int dataStructureTotal = 0;
int count = 0;
Node temp = head;
while (temp != null) {
chineseTotal += temp.student.chinese;
mathTotal += temp.student.math;
englishTotal += temp.student.english;
computerTotal += temp.student.computer;
dataStructureTotal += temp.student.dataStructure;
count++;
temp = temp.next;
}
System.out.println("Chinese average score: " + (double) chineseTotal / count);
System.out.println("Math average score: " + (double) mathTotal / count);
System.out.println("English average score: " + (double) englishTotal / count);
System.out.println("Computer average score: " + (double) computerTotal / count);
System.out.println("Data Structure average score: " + (double) dataStructureTotal / count);
}
public void displayFailedStudentsBySubject(String subject) {
Node temp = head;
while (temp != null) {
switch (subject.toLowerCase()) {
case "chinese":
if (temp.student.chinese < 60) {
System.out.println(temp.student.name);
}
break;
case "math":
if (temp.student.math < 60) {
System.out.println(temp.student.name);
}
break;
case "english":
if (temp.student.english < 60) {
System.out.println(temp.student.name);
}
break;
case "computer":
if (temp.student.computer < 60) {
System.out.println(temp.student.name);
}
break;
case "data structure":
if (temp.student.dataStructure < 60) {
System.out.println(temp.student.name);
}
break;
default:
System.out.println("Invalid subject!");
return;
}
temp = temp.next;
}
}
public void displayTotalScore() {
Node temp = head;
while (temp != null) {
temp.student.display();
System.out.println("Total Score: " + temp.student.totalScore());
System.out.println();
temp = temp.next;
}
}
}
public class Main {
public static void main(String[] args) {
LinkedList list = new LinkedList();
Student student1 = new Student("John", 1, "Class 1", "Male", 85, 90, 80, 70, 75);
Student student2 = new Student("Mary", 2, "Class 1", "Female", 75, 80, 90, 85, 80);
Student student3 = new Student("Tom", 3, "Class 2", "Male", 65, 70, 75, 80, 85);
Student student4 = new Student("Jane", 4, "Class 2", "Female", 90, 85, 75, 80, 70);
Student student5 = new Student("Bob", 5, "Class 3", "Male", 70, 75, 80, 85, 90);
Student student6 = new Student("Alice", 6, "Class 3", "Female", 80, 70, 75, 90, 85);
list.add(student1);
list.add(student2);
list.add(student3);
list.add(student4);
list.add(student5);
list.add(student6);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Display all students");
System.out.println("2. Display student by ID");
System.out.println("3. Display failed students");
System.out.println("4. Display average score by subject");
System.out.println("5. Display failed students by subject");
System.out.println("6. Display total score for all students");
System.out.println("7. Quit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
list.display();
break;
case 2:
System.out.print("Enter student ID: ");
int id = scanner.nextInt();
list.displayStudentById(id);
break;
case 3:
list.displayFailedStudents();
break;
case 4:
System.out.println("Chinese");
System.out.println("Math");
System.out.println("English");
System.out.println("Computer");
System.out.println("Data Structure");
System.out.print("Enter subject: ");
String subject = scanner.next();
list.displayAverageScore(subject);
break;
case 5:
System.out.println("Chinese");
System.out.println("Math");
System.out.println("English");
System.out.println("Computer");
System.out.println("Data Structure");
System.out.print("Enter subject: ");
subject = scanner.next();
list.displayFailedStudentsBySubject(subject);
break;
case 6:
list.displayTotalScore();
break;
case 7:
return;
default:
System.out.println("Invalid choice!");
break;
}
}
}
}
```
运行程序后,可以根据提示输入相应的选项,进行学生成绩管理系统的功能操作。
阅读全文