完成四则运算登陆模块,记录用户的四则运算历史得分,实现查询历史得分,使用 Java 序列化和反序列化进行持久化存储学生信息和得分情况,完整代码
时间: 2023-07-18 14:30:46 浏览: 87
基于Java的学生管理系统,旨在提供学生信息的管理和查询功能
好的,以下是一个完整的示例代码,包括用户登录模块、四则运算练习模块以及使用 Java 序列化和反序列化进行持久化存储学生信息和得分情况的模块。
```java
import java.io.*;
// 用户类,用于表示一个用户的信息
class User implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
// 题目类,用于表示一个四则运算题目
class Question implements Serializable {
private static final long serialVersionUID = 1L;
private String content;
private int answer;
public Question(String content, int answer) {
this.content = content;
this.answer = answer;
}
public String getContent() {
return content;
}
public int getAnswer() {
return answer;
}
// 随机生成一个四则运算题目
public static Question generateQuestion() {
int num1 = (int) (Math.random() * 100);
int num2 = (int) (Math.random() * 100);
int operator = (int) (Math.random() * 4);
String content;
int answer;
switch (operator) {
case 0:
content = num1 + " + " + num2 + " = ";
answer = num1 + num2;
break;
case 1:
content = num1 + " - " + num2 + " = ";
answer = num1 - num2;
break;
case 2:
content = num1 + " * " + num2 + " = ";
answer = num1 * num2;
break;
case 3:
content = num1 + " / " + num2 + " = ";
answer = num1 / num2;
break;
default:
content = "";
answer = 0;
break;
}
return new Question(content, answer);
}
}
// 得分类,用于表示一个学生的得分情况
class Score implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private int score;
public Score(String username, int score) {
this.username = username;
this.score = score;
}
public String getUsername() {
return username;
}
public int getScore() {
return score;
}
// 保存得分信息到文件中
public static void saveScore(Score score) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(score.getUsername() + ".dat"));
oos.writeObject(score);
oos.close();
}
// 从文件中读取得分信息
public static Score readScore(String username) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(username + ".dat"));
Score score = (Score) ois.readObject();
ois.close();
return score;
}
}
// 主程序类
public class Main {
private static User currentUser = null; // 当前用户
private static int currentScore = 0; // 当前得分
public static void main(String[] args) {
// 显示登录界面
LoginFrame loginFrame = new LoginFrame();
loginFrame.setVisible(true);
}
// 用户登录
public static boolean login(String username, String password) {
// 检查用户名和密码是否正确
User user = checkUser(username, password);
if (user != null) {
// 登录成功,记录当前用户
currentUser = user;
// 显示主界面
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
return true;
}
return false;
}
// 用户退出
public static void logout() {
// 记录得分信息到文件中
try {
Score.saveScore(new Score(currentUser.getUsername(), currentScore));
} catch (IOException e) {
e.printStackTrace();
}
// 重置当前用户和得分
currentUser = null;
currentScore = 0;
// 显示登录界面
LoginFrame loginFrame = new LoginFrame();
loginFrame.setVisible(true);
}
// 检查用户名和密码是否正确
private static User checkUser(String username, String password) {
// TODO: 实现用户验证逻辑,这里简单地返回一个固定的用户
if (username.equals("admin") && password.equals("123456")) {
return new User(username, password);
}
return null;
}
// 开始练习
public static void startExercise() {
// 显示题目界面
QuestionFrame questionFrame = new QuestionFrame();
questionFrame.setVisible(true);
// 生成第一道题目
generateNextQuestion();
}
// 生成下一道题目
public static void generateNextQuestion() {
// 生成一个新的题目
Question question = Question.generateQuestion();
// 在题目界面中显示题目内容
QuestionFrame.updateQuestion(question.getContent());
}
// 计算得分
public static void calculateScore(int answer) {
// 获取当前题目的正确答案
int correctAnswer = Question.generateQuestion().getAnswer();
// 如果用户的答案与正确答案相同,则得分加 1
if (answer == correctAnswer) {
currentScore++;
}
}
// 查询历史得分
public static int queryScore(String username) {
// 从文件中读取得分信息
try {
Score score = Score.readScore(username);
return score.getScore();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return 0;
}
}
// 登录界面类
class LoginFrame extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
public LoginFrame() {
setTitle("登录");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
add(panel);
panel.setLayout(null);
JLabel userLabel = new JLabel("用户名:");
userLabel.setBounds(10, 10, 80, 25);
panel.add(userLabel);
usernameField = new JTextField(20);
usernameField.setBounds(100, 10, 160, 25);
panel.add(usernameField);
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setBounds(10, 40, 80, 25);
panel.add(passwordLabel);
passwordField = new JPasswordField(20);
passwordField.setBounds(100, 40, 160, 25);
panel.add(passwordField);
JButton loginButton = new JButton("登录");
loginButton.setBounds(60, 80, 80, 25);
loginButton.addActionListener(e -> {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (Main.login(username, password)) {
dispose();
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
});
panel.add(loginButton);
JButton cancelButton = new JButton("取消");
cancelButton.setBounds(160, 80, 80, 25);
cancelButton.addActionListener(e -> {
dispose();
});
panel.add(cancelButton);
}
}
// 主界面类
class MainFrame extends JFrame {
public MainFrame() {
setTitle("四则运算练习");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
add(panel);
panel.setLayout(null);
JLabel welcomeLabel = new JLabel("欢迎你," + Main.currentUser.getUsername());
welcomeLabel.setBounds(10, 10, 200, 25);
panel.add(welcomeLabel);
JButton startButton = new JButton("开始练习");
startButton.setBounds(10, 40, 120, 25);
startButton.addActionListener(e -> {
Main.startExercise();
dispose();
});
panel.add(startButton);
JButton queryButton = new JButton("查询历史得分");
queryButton.setBounds(140, 40, 140, 25);
queryButton.addActionListener(e -> {
int score = Main.queryScore(Main.currentUser.getUsername());
JOptionPane.showMessageDialog(null, "你的历史得分是:" + score);
});
panel.add(queryButton);
JButton logoutButton = new JButton("退出登录");
logoutButton.setBounds(10, 80, 120, 25);
logoutButton.addActionListener(e -> {
Main.logout();
dispose();
});
panel.add(logoutButton);
}
}
// 题目界面类
class QuestionFrame extends JFrame {
private static JTextArea questionArea;
private JTextField answerField;
public QuestionFrame() {
setTitle("四则运算练习");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
add(panel);
panel.setLayout(null);
JLabel questionLabel = new JLabel("题目:");
questionLabel.setBounds(10, 10, 80, 25);
panel.add(questionLabel);
questionArea = new JTextArea();
questionArea.setBounds(100, 10, 160, 50);
questionArea.setEditable(false);
panel.add(questionArea);
JLabel answerLabel = new JLabel("答案:");
answerLabel.setBounds(10, 70, 80, 25);
panel.add(answerLabel);
answerField = new JTextField(20);
answerField.setBounds(100, 70, 160, 25);
panel.add(answerField);
JButton submitButton = new JButton("提交");
submitButton.setBounds(100, 105, 80, 25);
submitButton.addActionListener(e -> {
try {
int answer = Integer.parseInt(answerField.getText());
Main.calculateScore(answer);
Main.generateNextQuestion();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "请输入正确的数字");
}
});
panel.add(submitButton);
}
public static void updateQuestion(String question) {
questionArea.setText(question);
}
}
```
在上面的代码中,我们创建了三个类:User、Question 和 Score。User 类表示一个用户的信息,Question 类表示一个四则运算题目,Score 类表示一个学生的得分情况。我们还创建了一个主程序类 Main,用于协调用户登录、四则运算练习以及得分查询等功能。
在 Main 类中,我们使用了 Java Swing 来创建了三个界面:登录界面 LoginFrame、主界面 MainFrame 和题目界面 QuestionFrame。在登录界面中,我们使用 JTextField 和 JPasswordField 来获取用户输入的用户名和密码,然后调用
阅读全文