学生成绩管理系统JAVA
时间: 2023-12-31 20:23:57 浏览: 92
基于JAVA的学生成绩管理系统源代码.doc
学生成绩管理系统是一个用于录入、查询和输出学生成绩的系统。根据提供的引用内容,该系统具有四个功能:录入学生成绩、查询学生成绩、输出学生按成绩的排名和输出学科的分数。其中,后两个功能在"输出成绩"模块下。
以下是一个使用Java编写的学生成绩管理系统的简单示例:
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class StudentGradeManagementSystem {
private Map<String, Map<String, Integer>> gradeDatabase;
public StudentGradeManagementSystem() {
gradeDatabase = new HashMap<>();
}
public void addGrade(String studentName, String subject, int grade) {
if (!gradeDatabase.containsKey(studentName)) {
gradeDatabase.put(studentName, new HashMap<>());
}
gradeDatabase.get(studentName).put(subject, grade);
}
public void queryGrade(String studentName) {
if (gradeDatabase.containsKey(studentName)) {
System.out.println("Student: " + studentName);
System.out.println("Grades:");
Map<String, Integer> grades = gradeDatabase.get(studentName);
for (Map.Entry<String, Integer> entry : grades.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
} else {
System.out.println("Student not found.");
}
}
public void outputRanking() {
System.out.println("Ranking:");
gradeDatabase.entrySet().stream()
.sorted((e1, e2) -> getTotalGrade(e2.getValue()) - getTotalGrade(e1.getValue()))
.forEach(entry -> System.out.println(entry.getKey() + ": " + getTotalGrade(entry.getValue())));
}
public void outputSubjectGrades(String subject) {
System.out.println("Subject: " + subject);
gradeDatabase.entrySet().stream()
.filter(entry -> entry.getValue().containsKey(subject))
.forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue().get(subject)));
}
private int getTotalGrade(Map<String, Integer> grades) {
int totalGrade = 0;
for (int grade : grades.values()) {
totalGrade += grade;
}
return totalGrade;
}
public static void main(String[] args) {
StudentGradeManagementSystem system = new StudentGradeManagementSystem();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Please select an operation:");
System.out.println("1. Add grade");
System.out.println("2. Query grade");
System.out.println("3. Output ranking");
System.out.println("4. Output subject grades");
System.out.println("0. Exit");
int operation = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (operation) {
case 1:
System.out.println("Enter student name:");
String studentName = scanner.nextLine();
System.out.println("Enter subject:");
String subject = scanner.nextLine();
System.out.println("Enter grade:");
int grade = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
system.addGrade(studentName, subject, grade);
break;
case 2:
System.out.println("Enter student name:");
studentName = scanner.nextLine();
system.queryGrade(studentName);
break;
case 3:
system.outputRanking();
break;
case 4:
System.out.println("Enter subject:");
subject = scanner.nextLine();
system.outputSubjectGrades(subject);
break;
case 0:
System.exit(0);
break;
default:
System.out.println("Invalid operation.");
break;
}
}
}
}
```
这个示例中,我们使用了一个`Map`来存储学生的成绩信息,其中学生姓名作为键,对应的成绩信息作为值。系统提供了添加成绩、查询成绩、输出排名和输出学科成绩的功能。用户可以根据提示选择相应的操作,并根据需要输入相关信息。
阅读全文