10.24
package 测试;
import java.util.;
import javax.swing.JOptionPane;
import java.io.;
public class MathTest {
static final Random R = new Random();
// 存储题目和答案
static List
static List
// 存储用户答案
static List
// 存储错题索引
static List
public static void main(String[] args) {
generateAndSaveQuestions();
List<Double> userInputs = getAndCheckAnswers();
// 统计结果
int wrongCount = 0;
StringBuilder resultMessage = new StringBuilder();
for (int i = 0; i < questions.size(); i++) {
double std = answers.get(i);
double userAnswer = userInputs.get(i);
String feedback;
if (Math.abs(userAnswer - std) < 1e-6) {
feedback = "正确";
} else {
feedback = "错误,正确答案:" + (std % 1 == 0 ? (long) std : std);
wrongCount++;
wrongIndexes.add(i);
}
resultMessage.append((i + 1)).append(". ").append(questions.get(i)).append(userAnswer).append(" → ").append(feedback).append("\n");
}
double accuracy = (questions.size() - wrongCount) * 100.0 / questions.size();
resultMessage.append("\n------------------\n")
.append(String.format("错题数:%d / %d,正确率:%.2f%%", wrongCount, questions.size(), accuracy));
JOptionPane.showMessageDialog(null, resultMessage.toString(), "测验结果", JOptionPane.INFORMATION_MESSAGE);
// 错题二次答题
if (!wrongIndexes.isEmpty()) {
int choice = JOptionPane.showConfirmDialog(null, "是否进行错题二次答题?", "提示", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
redoWrongQuestions();
}
}
// 数据库连接(这里只是示例,需根据实际数据库修改)
if (connectDatabase()) {
JOptionPane.showMessageDialog(null, "数据库连接成功,直接过关!", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "数据库连接失败。", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
// 生成题目并保存到文本文件
private static void generateAndSaveQuestions() {
try (PrintWriter writer = new PrintWriter(new FileWriter("math_questions.txt"))) {
StringBuilder line = new StringBuilder();
for (int i = 1; i <= 30; i++) {
String exp = generateExpression();
double std = calculate(exp);
questions.add(exp);
answers.add(std);
line.append(exp).append(" ");
if (i % 3 == 0) {
writer.println(line.toString().trim());
line = new StringBuilder();
}
}
if (line.length() > 0) {
writer.println(line.toString().trim());
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "题目保存失败:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
// 获取用户答案并判读
private static List<Double> getAndCheckAnswers() {
List<Double> userInputs = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("math_questions.txt"))) {
String line;
int questionIndex = 0;
while ((line = reader.readLine()) != null && questionIndex < 30) {
String[] qs = line.split(" ");
for (String q : qs) {
if (questionIndex >= 30) break;
String exp = q;
double std = calculate(exp);
String userInput = JOptionPane.showInputDialog(null,
(questionIndex + 1) + ". " + exp,
"数学测验 - 第" + (questionIndex + 1) + "题",
JOptionPane.QUESTION_MESSAGE);
if (userInput == null) {
JOptionPane.showMessageDialog(null, "测验已取消", "提示", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
double userAnswer;
try {
userAnswer = Double.parseDouble(userInput);
} catch (NumberFormatException e) {
userAnswer = Double.NaN;
}
userInputs.add(userAnswer);
questionIndex++;
}
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "题目读取失败:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
return userInputs;
}
// 错题二次答题
private static void redoWrongQuestions() {
int newWrongCount = 0;
StringBuilder redoResult = new StringBuilder();
redoResult.append("错题二次答题结果:\n");
for (int index : wrongIndexes) {
String exp = questions.get(index);
double std = answers.get(index);
String userInput = JOptionPane.showInputDialog(null,
"错题重答 - " + (index + 1) + ". " + exp,
"数学测验 - 错题重答",
JOptionPane.QUESTION_MESSAGE);
if (userInput == null) break;
double userAnswer;
try {
userAnswer = Double.parseDouble(userInput);
} catch (NumberFormatException e) {
userAnswer = Double.NaN;
}
String feedback;
if (Math.abs(userAnswer - std) < 1e-6) {
feedback = "正确";
} else {
feedback = "错误,正确答案:" + (std % 1 == 0 ? (long) std : std);
newWrongCount++;
}
redoResult.append((index + 1)).append(". ").append(exp).append(userAnswer).append(" → ").append(feedback).append("\n");
}
redoResult.append("\n二次答题后,仍错误数:").append(newWrongCount);
JOptionPane.showMessageDialog(null, redoResult.toString(), "错题二次答题结果", JOptionPane.INFORMATION_MESSAGE);
}
// 数据库连接(示例,需根据实际数据库修改)
private static boolean connectDatabase() {
// 这里只是模拟数据库连接,实际需使用JDBC等方式连接真实数据库
// 例如:
// try {
// Class.forName("com.mysql.jdbc.Driver");
// Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", "username", "password");
// conn.close();
// return true;
// } catch (Exception e) {
// return false;
// }
// 现在简单返回true模拟连接成功
return true;
}
private static String generateExpression() {
int a = 0, b = 0, opType;
while (true) {
opType = R.nextInt(4);
switch (opType) {
case 0:
a = 1 + R.nextInt(100);
b = 1 + R.nextInt(100);
break;
case 1:
a = 1 + R.nextInt(100);
b = 1 + R.nextInt(a);
break;
case 2:
a = 1 + R.nextInt(31);
b = 1 + R.nextInt(31);
if (a * b > 999) continue;
break;
case 3:
b = 1 + R.nextInt(100);
int q = 1 + R.nextInt(100 / b + 1);
a = q * b;
break;
}
break;
}
char operator = "+-*/".charAt(opType);
return "" + a + operator + b + "=";
}
private static double calculate(String exp) {
String[] parts = exp.split("[-+*/=]");
int a = Integer.parseInt(parts[0]);
int b = Integer.parseInt(parts[1]);
char op = exp.replaceAll("[0-9]|=", "").charAt(0);
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return (double) a / b;
default:
return 0;
}
}
}

浙公网安备 33010602011771号