如何打代码在java中在四则运算part1的基础上,增加登陆模块(至少需要2个用户,可以不实现注册功能),和记录具体某位用户的四则运算历史得分(需要有序号,第几次,得分时间),需要进行持久化存储(IO文件存储,或者数据库存储)
时间: 2024-03-18 19:40:34 浏览: 89
好的,以下是Java代码的实现:
首先是用户类:
```java
public class User {
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;
}
}
```
接下来是四则运算类:
```java
import java.util.Random;
public class Arithmetic {
private static final int MAX_NUM = 100; // 题目数值的最大值
private static final int MAX_OPERATOR = 4; // 运算符的数量
private static final String[] OPERATORS = {"+", "-", "*", "/"}; // 运算符数组
private int num1;
private int num2;
private String operator;
private int answer;
public Arithmetic() {
Random random = new Random();
num1 = random.nextInt(MAX_NUM) + 1;
num2 = random.nextInt(MAX_NUM) + 1;
operator = OPERATORS[random.nextInt(MAX_OPERATOR)];
answer = calculate();
}
private int calculate() {
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
return num1 / num2;
default:
return 0;
}
}
public String getQuestion() {
return num1 + " " + operator + " " + num2 + " = ?";
}
public boolean checkAnswer(int userAnswer) {
return answer == userAnswer;
}
public int getAnswer() {
return answer;
}
}
```
然后是历史记录类:
```java
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Record {
private List<Integer> scores;
public Record() {
scores = new ArrayList<>();
}
public void addScore(int score) {
scores.add(score);
}
public void printScores() {
System.out.println("历史记录:");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (int i = 0; i < scores.size(); i++) {
System.out.println("第" + (i+1) + "次:" + scores.get(i) + "分\t" + sdf.format(new Date()));
}
}
}
```
接下来是数据库类:
```java
import java.sql.*;
public class Database {
private static final String URL = "jdbc:mysql://localhost:3306/arithmetic";
private static final String USER = "root";
private static final String PASSWORD = "123456";
private Connection conn;
public Database() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public boolean login(String username, String password) {
try {
String sql = "select * from user where username = ? and password = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public Record getRecord(String username) {
try {
String sql = "select score from record where username = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
Record record = new Record();
while (rs.next()) {
record.addScore(rs.getInt("score"));
}
return record;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public void addRecord(String username, int score) {
try {
String sql = "insert into record(username, score) values(?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setInt(2, score);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
最后是主程序类:
```java
import java.util.Scanner;
public class Main {
private static final int MAX_QUESTION = 10; // 最大题目数
private static final int PASS_SCORE = 60; // 及格分数
private static User currentUser;
private static Record record;
private static Database database = new Database();
public static void main(String[] args) {
System.out.println("欢迎使用四则运算程序!");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入用户名:");
String username = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
if (database.login(username, password)) {
System.out.println("登录成功!");
currentUser = new User(username, password);
record = database.getRecord(username);
break;
} else {
System.out.println("用户名或密码错误,请重新输入!");
}
}
System.out.println("请选择难度:1.简单(数字范围1~10) 2.中等(数字范围1~100) 3.困难(数字范围1~1000)");
int level = scanner.nextInt();
int score = 0;
Arithmetic[] questions = generateQuestions(level);
for (int i = 0; i < questions.length; i++) {
System.out.println("第" + (i+1) + "题:" + questions[i].getQuestion());
int userAnswer = scanner.nextInt();
if (questions[i].checkAnswer(userAnswer)) {
System.out.println("回答正确!");
score += 10;
} else {
System.out.println("回答错误!");
}
}
System.out.println("本次得分:" + score);
record.addScore(score);
if (score >= PASS_SCORE) {
System.out.println("恭喜您,本次测试通过!");
} else {
System.out.println("很遗憾,本次测试未通过!");
}
database.addRecord(currentUser.getUsername(), score);
record.printScores();
}
private static Arithmetic[] generateQuestions(int level) {
Arithmetic[] questions = new Arithmetic[MAX_QUESTION];
for (int i = 0; i < MAX_QUESTION; i++) {
questions[i] = new Arithmetic();
}
return questions;
}
}
```
以上代码是基于Java语言实现的四则运算程序,包含了登录模块和历史记录功能,并且使用了数据库来进行数据存储和读取操作。其中,用户类用于保存用户信息,四则运算类用于生成题目、计算答案和判断答案是否正确等操作,历史记录类用于保存每次用户的四则运算得分记录,数据库类用于连接数据库并进行数据存储和读取操作,主程序类用于实现用户登录、四则运算、历史记录等功能。
阅读全文