用java编写一个学生成绩管理系统 ,主要功能: 1、输入一个班级的学生的基本信息(包括学号,姓名,性别,5门课程成绩)。 2、按姓名或者学号查找、修改、删除和保存各个学生的信息。 3、计算每个学生各门功课总分和平均分,按学号或总分排序输出每个学生的基本信息及总分、平均分和名次。 4、计算全班各门功课的平均分,显示每门课程中低于平均分的每一个学生的学号,姓名,性别,科目,成绩。 5、显示每门科目中,成绩在90分以上的学生信息,以及每门科目中不及格的学生信息。 6、用菜单进行管理 7、只有正确输入用户名密码才能使用此系统。 8、所有内容能够保存记事本文件中。下次进入系统是从记事本文件中读取原有信息。
时间: 2023-11-27 22:51:36 浏览: 51
以下是一个简单的学生成绩管理系统的Java代码示例:
```java
import java.io.*;
import java.util.*;
public class StudentManagementSystem {
private static final String USERNAME = "admin";
private static final String PASSWORD = "123456";
private static final String FILE_NAME = "students.txt";
private static List<Student> students = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
login();
loadStudentsFromFile();
boolean running = true;
while (running) {
printMenu();
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
findStudent();
break;
case 3:
modifyStudent();
break;
case 4:
deleteStudent();
break;
case 5:
sortStudents();
break;
case 6:
showLowScores();
break;
case 7:
showHighScores();
break;
case 8:
saveStudentsToFile();
running = false;
break;
default:
System.out.println("Invalid choice!");
}
}
System.out.println("Goodbye!");
}
private static void login() {
System.out.println("Please enter username:");
String username = scanner.next();
System.out.println("Please enter password:");
String password = scanner.next();
if (!username.equals(USERNAME) || !password.equals(PASSWORD)) {
System.out.println("Incorrect username or password!");
System.exit(0);
}
}
private static void loadStudentsFromFile() {
try {
FileReader fileReader = new FileReader(FILE_NAME);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(",");
String id = parts[0];
String name = parts[1];
String gender = parts[2];
int[] scores = new int[5];
for (int i = 3; i < 8; i++) {
scores[i - 3] = Integer.parseInt(parts[i]);
}
Student student = new Student(id, name, gender, scores);
students.add(student);
}
bufferedReader.close();
fileReader.close();
} catch (IOException e) {
System.out.println("Error reading file!");
}
}
private static void saveStudentsToFile() {
try {
FileWriter fileWriter = new FileWriter(FILE_NAME);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (Student student : students) {
bufferedWriter.write(student.toString());
bufferedWriter.newLine();
}
bufferedWriter.close();
fileWriter.close();
} catch (IOException e) {
System.out.println("Error writing file!");
}
}
private static void addStudent() {
System.out.println("Please enter student id:");
String id = scanner.next();
System.out.println("Please enter student name:");
String name = scanner.next();
System.out.println("Please enter student gender:");
String gender = scanner.next();
int[] scores = new int[5];
System.out.println("Please enter scores for 5 courses:");
for (int i = 0; i < 5; i++) {
scores[i] = scanner.nextInt();
}
Student student = new Student(id, name, gender, scores);
students.add(student);
System.out.println("Student added successfully!");
}
private static void findStudent() {
System.out.println("Please enter student id or name:");
String keyword = scanner.next();
boolean found = false;
for (Student student : students) {
if (student.getId().equals(keyword) || student.getName().equals(keyword)) {
System.out.println(student);
found = true;
}
}
if (!found) {
System.out.println("Student not found!");
}
}
private static void modifyStudent() {
System.out.println("Please enter student id:");
String id = scanner.next();
boolean found = false;
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println("Please enter new name (press enter to skip):");
String name = scanner.nextLine();
if (!name.isEmpty()) {
student.setName(name);
}
System.out.println("Please enter new gender (press enter to skip):");
String gender = scanner.nextLine();
if (!gender.isEmpty()) {
student.setGender(gender);
}
System.out.println("Please enter new scores for 5 courses (press enter to skip):");
String scoresStr = scanner.nextLine();
if (!scoresStr.isEmpty()) {
String[] scoresParts = scoresStr.split(",");
int[] scores = new int[5];
for (int i = 0; i < 5; i++) {
scores[i] = Integer.parseInt(scoresParts[i]);
}
student.setScores(scores);
}
System.out.println("Student modified successfully!");
found = true;
break;
}
}
if (!found) {
System.out.println("Student not found!");
}
}
private static void deleteStudent() {
System.out.println("Please enter student id:");
String id = scanner.next();
boolean found = false;
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
students.remove(i);
System.out.println("Student deleted successfully!");
found = true;
break;
}
}
if (!found) {
System.out.println("Student not found!");
}
}
private static void sortStudents() {
System.out.println("Sort by (1. id, 2. total score):");
int choice = scanner.nextInt();
if (choice == 1) {
Collections.sort(students, Comparator.comparing(Student::getId));
} else if (choice == 2) {
Collections.sort(students, Comparator.comparing(Student::getTotalScore).reversed());
} else {
System.out.println("Invalid choice!");
return;
}
int position = 1;
for (Student student : students) {
System.out.println(position + ". " + student);
position++;
}
}
private static void showLowScores() {
for (int i = 0; i < 5; i++) {
int sum = 0;
int count = 0;
for (Student student : students) {
int score = student.getScores()[i];
sum += score;
count++;
}
double average = (double) sum / count;
System.out.println("Course " + (i + 1) + " average score: " + average);
System.out.println("Students with score below average:");
for (Student student : students) {
int score = student.getScores()[i];
if (score < average) {
System.out.println(student);
}
}
}
}
private static void showHighScores() {
for (int i = 0; i < 5; i++) {
System.out.println("Students with score above 90 in course " + (i + 1) + ":");
for (Student student : students) {
int score = student.getScores()[i];
if (score > 90) {
System.out.println(student);
}
}
System.out.println("Students with score below 60 in course " + (i + 1) + ":");
for (Student student : students) {
int score = student.getScores()[i];
if (score < 60) {
System.out.println(student);
}
}
}
}
private static void printMenu() {
System.out.println("1. Add student");
System.out.println("2. Find student");
System.out.println("3. Modify student");
System.out.println("4. Delete student");
System.out.println("5. Sort students");
System.out.println("6. Show low scores");
System.out.println("7. Show high scores");
System.out.println("8. Save and exit");
System.out.println("Please enter your choice:");
}
}
class Student {
private String id;
private String name;
private String gender;
private int[] scores;
public Student(String id, String name, String gender, int[] scores) {
this.id = id;
this.name = name;
this.gender = gender;
this.scores = scores;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int[] getScores() {
return scores;
}
public void setScores(int[] scores) {
this.scores = scores;
}
public int getTotalScore() {
int sum = 0;
for (int score : scores) {
sum += score;
}
return sum;
}
@Override
public String toString() {
return id + "," + name + "," + gender + "," + scores[0] + "," + scores[1] + "," + scores[2] + "," + scores[3] + "," + scores[4];
}
}
```
注意,这只是一个简单的示例,实际应用中可能需要更多的功能和更严格的输入验证。