怎样使用java语言制作一个简单的学生成绩管理系统
时间: 2023-07-15 19:11:05 浏览: 98
制作一个简单的学生成绩管理系统,需要按照以下步骤进行:
1. 设计数据结构
首先,需要设计数据结构来存储学生的信息和成绩。可以使用一个学生类来表示每个学生,其中包含姓名、学号、班级、成绩等属性。
2. 实现界面
其次,需要实现一个界面来展示学生信息和成绩。可以使用Java Swing或JavaFX来实现界面。
3. 实现功能
最后,需要实现以下功能:
- 添加学生:将新学生添加到系统中。
- 删除学生:从系统中删除学生。
- 修改学生信息:修改学生的姓名、学号、班级等信息。
- 添加成绩:为学生添加成绩。
- 修改成绩:修改学生的成绩。
- 查询学生信息:根据姓名、学号等条件查询学生信息。
- 统计成绩:计算学生的平均成绩、最高分、最低分等统计信息。
参考代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class StudentManagementSystem extends JFrame implements ActionListener {
private JTextField nameField, idField, classField, scoreField;
private JButton addButton, deleteButton, modifyButton, queryButton, statButton;
private JTextArea resultArea;
private ArrayList<Student> studentList;
public StudentManagementSystem() {
super("学生成绩管理系统");
setSize(600, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
studentList = new ArrayList<Student>();
JPanel inputPanel = new JPanel(new GridLayout(5, 2));
inputPanel.add(new JLabel("姓名:"));
nameField = new JTextField(20);
inputPanel.add(nameField);
inputPanel.add(new JLabel("学号:"));
idField = new JTextField(20);
inputPanel.add(idField);
inputPanel.add(new JLabel("班级:"));
classField = new JTextField(20);
inputPanel.add(classField);
inputPanel.add(new JLabel("成绩:"));
scoreField = new JTextField(20);
inputPanel.add(scoreField);
addButton = new JButton("添加");
addButton.addActionListener(this);
inputPanel.add(addButton);
deleteButton = new JButton("删除");
deleteButton.addActionListener(this);
inputPanel.add(deleteButton);
modifyButton = new JButton("修改");
modifyButton.addActionListener(this);
inputPanel.add(modifyButton);
queryButton = new JButton("查询");
queryButton.addActionListener(this);
inputPanel.add(queryButton);
statButton = new JButton("统计");
statButton.addActionListener(this);
inputPanel.add(statButton);
resultArea = new JTextArea(10, 40);
resultArea.setEditable(false);
Container contentPane = getContentPane();
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(new JScrollPane(resultArea), BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText().trim();
String id = idField.getText().trim();
String className = classField.getText().trim();
String scoreStr = scoreField.getText().trim();
if (name.equals("") || id.equals("") || className.equals("") || scoreStr.equals("")) {
JOptionPane.showMessageDialog(this, "请输入完整信息", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
try {
double score = Double.parseDouble(scoreStr);
Student student = new Student(name, id, className, score);
studentList.add(student);
resultArea.setText("添加成功\n" + student.toString());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "成绩格式错误", "错误", JOptionPane.ERROR_MESSAGE);
}
} else if (e.getSource() == deleteButton) {
String id = idField.getText().trim();
boolean deleted = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
studentList.remove(i);
deleted = true;
break;
}
}
if (deleted) {
resultArea.setText("删除成功");
} else {
resultArea.setText("未找到该学生");
}
} else if (e.getSource() == modifyButton) {
String id = idField.getText().trim();
boolean modified = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
String name = nameField.getText().trim();
String className = classField.getText().trim();
String scoreStr = scoreField.getText().trim();
if (!name.equals("")) {
student.setName(name);
}
if (!className.equals("")) {
student.setClassName(className);
}
if (!scoreStr.equals("")) {
try {
double score = Double.parseDouble(scoreStr);
student.setScore(score);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "成绩格式错误", "错误", JOptionPane.ERROR_MESSAGE);
}
}
modified = true;
break;
}
}
if (modified) {
resultArea.setText("修改成功");
} else {
resultArea.setText("未找到该学生");
}
} else if (e.getSource() == queryButton) {
String name = nameField.getText().trim();
String id = idField.getText().trim();
if (!name.equals("")) {
boolean found = false;
for (Student student : studentList) {
if (student.getName().equals(name)) {
resultArea.setText(student.toString());
found = true;
break;
}
}
if (!found) {
resultArea.setText("未找到该学生");
}
} else if (!id.equals("")) {
boolean found = false;
for (Student student : studentList) {
if (student.getId().equals(id)) {
resultArea.setText(student.toString());
found = true;
break;
}
}
if (!found) {
resultArea.setText("未找到该学生");
}
} else {
resultArea.setText("请输入姓名或学号");
}
} else if (e.getSource() == statButton) {
double sum = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
int count = studentList.size();
for (Student student : studentList) {
double score = student.getScore();
sum += score;
max = Math.max(max, score);
min = Math.min(min, score);
}
double average = sum / count;
resultArea.setText("平均成绩:" + average + "\n最高分:" + max + "\n最低分:" + min);
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.setVisible(true);
}
}
class Student {
private String name;
private String id;
private String className;
private double score;
public Student(String name, String id, String className, double score) {
this.name = name;
this.id = id;
this.className = className;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String toString() {
return "姓名:" + name + "\n学号:" + id + "\n班级:" + className + "\n成绩:" + score;
}
}
```
注意:这只是一个简单的实现,还可以根据实际需求进行扩展。
阅读全文