Compare commits

..

5 Commits

@ -1,10 +0,0 @@
<component name="ArtifactManager">
<artifact type="jar" build-on-make="true" name="MathLearningSoftware:jar">
<output-path>$PROJECT_DIR$/out/artifacts/MathLearningSoftware_jar</output-path>
<root id="archive" name="MathLearningSoftware.jar">
<element id="module-output" name="MathLearningSoftware" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/com/sun/mail/javax.mail/1.6.2/javax.mail-1.6.2.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/javax/activation/activation/1.1/activation-1.1.jar" path-in-jar="/" />
</root>
</artifact>
</component>

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" project-jdk-name="23" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" project-jdk-name="23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -1,9 +1,4 @@
2023@qq.com|hahah|971458|Hsh2005|true
14498@qq.com|ly|862004||false
1449892469@qq.com|ly|137032|Ly2004|true
1577@163.com|txj|972753|Txj123456|true
2095885186@qq.com|null|060613||false
3045193232@qq.com|null|056850||false
38425875@qq.com|ewfguwfe|360732|nI1234|true
2768350566@qq.com|x|715819|Ni1234|true
2281638407@qq.com|guogu|214092|Guo*12|true
2023@qq.com|971458|Hsh2006|true
202326010221@168.com|829554||false
14498@qq.com|862004||false
1449892469@qq.com|137032|Ly2004|true

@ -1,46 +1,40 @@
# 数学学习软件
## 项目简介
这是一个基于Java Swing开发的数学学习软件旨在为不同学段的学生提供个性化的数学练习体验。软件支持用户注册、登录、密码管理以及根据小学、初中、高中不同难度生成相应的数学题目。
## 功能特性
### 用户管理
- **用户注册**: 通过邮箱注册,系统发送注册码验证
- **用户登录**: 安全的邮箱密码登录机制
- **密码管理**: 支持密码修改,密码强度验证
### 学习功能
- **难度选择**: 小学、初中、高中三个难度级别
- **题目生成**: 根据难度自动生成相应数学题目
- **答题系统**: 选择题形式,实时反馈
- **成绩统计**: 答题结果分析和详细报告
### 技术特点
- 基于Java Swing的图形界面
- 数据持久化存储
- 模块化设计,易于扩展
- 代码结构清晰,注释完整
## 系统要求
- JDK24 或更高版本
- 支持Java Swing的桌面环境
- 至少 100MB 可用磁盘空间
## 注意
- 发送验证码邮件的邮箱账号为```3602474328@qq.com```
- 包含验证码的邮件可能会被归类为垃圾邮件,若迟迟未收到验证码请到垃圾箱中翻找一下
- 用户名和密码都不能出现字符```|```,否则判定为非法
- 登录时使用的是邮箱+密码登录,不是用户名+密码
## 安装和运行
### 方法一使用预编译JAR文件
1. 下载项目的最新发布版本
2. 确保系统已安装Java运行环境
3. 双击JAR文件运行或使用命令行
```bash
# 数学学习软件
## 项目简介
这是一个基于Java Swing开发的数学学习软件旨在为不同学段的学生提供个性化的数学练习体验。软件支持用户注册、登录、密码管理以及根据小学、初中、高中不同难度生成相应的数学题目。
## 功能特性
### 用户管理
- **用户注册**: 通过邮箱注册,系统发送注册码验证
- **用户登录**: 安全的邮箱密码登录机制
- **密码管理**: 支持密码修改,密码强度验证
### 学习功能
- **难度选择**: 小学、初中、高中三个难度级别
- **题目生成**: 根据难度自动生成相应数学题目
- **答题系统**: 选择题形式,实时反馈
- **成绩统计**: 答题结果分析和详细报告
### 技术特点
- 基于Java Swing的图形界面
- JSON数据持久化存储
- 模块化设计,易于扩展
- 代码结构清晰,注释完整
## 系统要求
- Java 8 或更高版本
- 支持Java Swing的桌面环境
- 至少 100MB 可用磁盘空间
## 安装和运行
### 方法一使用预编译JAR文件
1. 下载项目的最新发布版本
2. 确保系统已安装Java运行环境
3. 双击JAR文件运行或使用命令行
```bash
java -jar MathLearningSoftware.jar

@ -1,3 +0,0 @@
Manifest-Version: 1.0
Main-Class: com.mathlearning.Main

@ -52,16 +52,7 @@ public class AuthController {
}
}
public boolean isUsernameAvailable(String username) {
User user = userService.getUserByUsername(username);
if (user != null && user.isRegistered()) {
return false;
} else {
return true;
}
}
public boolean completeRegistration(String email, String user_name, String password) {
public boolean completeRegistration(String email, String registrationCode, String password) {
try {
/*
if (!authService.validateRegistrationCode(email, registrationCode)) {
@ -70,7 +61,7 @@ public class AuthController {
*/
User user = userService.getUserByEmail(email);
if (user != null && authService.validatePassword(password)) {
return userService.completeRegistration(email, user_name, password);
return userService.completeRegistration(email, password);
}
return false;
} catch (Exception e) {
@ -137,10 +128,6 @@ public class AuthController {
return currentUser != null ? currentUser.getEmail() : null;
}
public String getCurrentUserName() {
return currentUser != null ? currentUser.getUser_name() : null;
}
public boolean isCodeRegistered(String email, String registrationCode) {
if (!authService.validateRegistrationCode(email, registrationCode)) {
return false;

@ -31,9 +31,9 @@ public class QuestionController {
System.out.println((i + 1) + ". " + q.getQuestionText());
String[] options = q.getOptions();
for (int j = 0; j < options.length; j++) {
System.out.println(" " + (char) ('A' + j) + ". " + options[j]);
System.out.println(" " + (char)('A' + j) + ". " + options[j]);
}
System.out.println(" 正确答案: " + (char) ('A' + q.getCorrectIndex()));
System.out.println(" 正确答案: " + (char)('A' + q.getCorrectIndex()));
System.out.println();
}
}
@ -137,4 +137,4 @@ public class QuestionController {
this.currentQuestionIndex = index;
}
}
}
}

@ -7,35 +7,35 @@ import java.util.Set;
public abstract class AbstractQuestionGenerator implements QuestionGenerator {
@Override
public List<Question> generateQuestions(String level, int count) {
List<Question> questions = new ArrayList<>();
Set<String> questionTexts = new HashSet<>();
for (int i = 0; i < count; i++) {
Question question = generateUniqueQuestion(level, i, questionTexts);
if (question != null) {
questions.add(question);
}
@Override
public List<Question> generateQuestions(String level, int count) {
List<Question> questions = new ArrayList<>();
Set<String> questionTexts = new HashSet<>();
for (int i = 0; i < count; i++) {
Question question = generateUniqueQuestion(level, i, questionTexts);
if (question != null) {
questions.add(question);
}
}
return questions;
}
return questions;
}
private Question generateUniqueQuestion(String level, int index, Set<String> questionTexts) {
Question question;
int attempt = 0;
do {
question = generateQuestion(level, index);
attempt++;
} while (question != null && questionTexts.contains(question.getQuestionText()) && attempt < 10);
private Question generateUniqueQuestion(String level, int index, Set<String> questionTexts) {
Question question;
int attempt = 0;
do {
question = generateQuestion(level, index);
attempt++;
} while (question != null && questionTexts.contains(question.getQuestionText()) && attempt < 10);
if (question != null) {
questionTexts.add(question.getQuestionText());
}
if (question != null) {
questionTexts.add(question.getQuestionText());
return question;
}
return question;
}
protected abstract Question generateQuestion(String level, int index);
protected abstract Question generateQuestion(String level, int index);
}

@ -7,186 +7,186 @@ import java.util.Random;
import java.util.Set;
public abstract class BaseQuestionGenerator {
protected Random random = new Random();
protected Random random = new Random();
public abstract Question generateQuestion(int index);
public abstract Question generateQuestion(int index);
protected void generateOperands(int[] operands, int min, int max) {
for (int i = 0; i < operands.length; i++) {
operands[i] = random.nextInt(max - min + 1) + min;
protected void generateOperands(int[] operands, int min, int max) {
for (int i = 0; i < operands.length; i++) {
operands[i] = random.nextInt(max - min + 1) + min;
}
}
}
protected void generateOperations(int[] operations, int operationTypes) {
for (int i = 0; i < operations.length; i++) {
operations[i] = random.nextInt(operationTypes);
protected void generateOperations(int[] operations, int operationTypes) {
for (int i = 0; i < operations.length; i++) {
operations[i] = random.nextInt(operationTypes);
}
}
}
protected void appendOperator(StringBuilder sb, int operation) {
switch (operation) {
case 0: sb.append(" + "); break;
case 1: sb.append(" - "); break;
case 2: sb.append(" × "); break;
case 3: sb.append(" ÷ "); break;
protected void appendOperator(StringBuilder sb, int operation) {
switch (operation) {
case 0: sb.append(" + "); break;
case 1: sb.append(" - "); break;
case 2: sb.append(" × "); break;
case 3: sb.append(" ÷ "); break;
}
}
}
protected boolean isValidAnswer(double answer) {
return !(answer < 0 || Double.isNaN(answer) || Double.isInfinite(answer));
}
protected boolean isValidAnswer(double answer) {
return !(answer < 0 || Double.isNaN(answer) || Double.isInfinite(answer));
}
// 修改使用double进行精确计算只在最后保留小数
protected double calculateExpression(double[] values, int[] operations) {
List<Double> numbers = new ArrayList<>();
List<Integer> ops = new ArrayList<>();
// 修改使用double进行精确计算只在最后保留小数
protected double calculateExpression(double[] values, int[] operations) {
List<Double> numbers = new ArrayList<>();
List<Integer> ops = new ArrayList<>();
for (double value : values) numbers.add(value);
for (int operation : operations) ops.add(operation);
for (double value : values) numbers.add(value);
for (int operation : operations) ops.add(operation);
processMultiplicationAndDivision(numbers, ops);
return processAdditionAndSubtraction(numbers, ops);
}
processMultiplicationAndDivision(numbers, ops);
return processAdditionAndSubtraction(numbers, ops);
}
protected double calculateWithoutParentheses(int[] operands, int[] operations) {
double[] doubleOperands = new double[operands.length];
for (int i = 0; i < operands.length; i++) {
doubleOperands[i] = operands[i];
protected double calculateWithoutParentheses(int[] operands, int[] operations) {
double[] doubleOperands = new double[operands.length];
for (int i = 0; i < operands.length; i++) {
doubleOperands[i] = operands[i];
}
return calculateExpression(doubleOperands, operations);
}
return calculateExpression(doubleOperands, operations);
}
protected double calculateWithoutParentheses(List<Integer> numbers, List<Integer> ops) {
double[] doubleNumbers = new double[numbers.size()];
for (int i = 0; i < numbers.size(); i++) {
doubleNumbers[i] = numbers.get(i);
protected double calculateWithoutParentheses(List<Integer> numbers, List<Integer> ops) {
double[] doubleNumbers = new double[numbers.size()];
for (int i = 0; i < numbers.size(); i++) {
doubleNumbers[i] = numbers.get(i);
}
int[] intOps = new int[ops.size()];
for (int i = 0; i < ops.size(); i++) {
intOps[i] = ops.get(i);
}
return calculateExpression(doubleNumbers, intOps);
}
int[] intOps = new int[ops.size()];
for (int i = 0; i < ops.size(); i++) {
intOps[i] = ops.get(i);
private void processMultiplicationAndDivision(List<Double> numbers, List<Integer> ops) {
for (int i = 0; i < ops.size(); i++) {
int op = ops.get(i);
if (op == 2 || op == 3) {
double left = numbers.get(i);
double right = numbers.get(i + 1);
double result = performOperation(left, right, op);
numbers.set(i, result);
numbers.remove(i + 1);
ops.remove(i);
i--;
}
}
}
return calculateExpression(doubleNumbers, intOps);
}
private double processAdditionAndSubtraction(List<Double> numbers, List<Integer> ops) {
double result = numbers.get(0);
private void processMultiplicationAndDivision(List<Double> numbers, List<Integer> ops) {
for (int i = 0; i < ops.size(); i++) {
int op = ops.get(i);
if (op == 2 || op == 3) {
double left = numbers.get(i);
double right = numbers.get(i + 1);
double result = performOperation(left, right, op);
for (int i = 0; i < ops.size(); i++) {
int op = ops.get(i);
double nextNum = numbers.get(i + 1);
result = performOperation(result, nextNum, op);
}
numbers.set(i, result);
numbers.remove(i + 1);
ops.remove(i);
i--;
}
// 修改只在最后一步进行四舍五入保留2位小数
return Math.round(result * 100) / 100.0;
}
}
private double processAdditionAndSubtraction(List<Double> numbers, List<Integer> ops) {
double result = numbers.get(0);
private double performOperation(double left, double right, int operation) {
switch (operation) {
case 0: return left + right;
case 1: return left - right;
case 2: return left * right;
case 3:
if (right == 0) return Double.NaN;
// 修改:直接进行除法,不在这里保留小数
return left / right;
default: return left;
}
}
protected Question generateOptions(String questionText, double answer, String level) {
String[] options = new String[4];
Set<String> usedValues = new HashSet<>();
// 修改:判断是否为整数的逻辑
boolean isIntegerAnswer = Math.abs(answer - Math.round(answer)) < 1e-10;
String correctAnswer = formatAnswer(answer, isIntegerAnswer);
options[0] = correctAnswer;
usedValues.add(correctAnswer);
generateWrongOptions(options, usedValues, answer, isIntegerAnswer);
shuffleArray(options);
for (int i = 0; i < ops.size(); i++) {
int op = ops.get(i);
double nextNum = numbers.get(i + 1);
result = performOperation(result, nextNum, op);
int correctIndex = findCorrectIndex(options, correctAnswer);
return new Question(questionText, options, correctIndex, level);
}
// 修改只在最后一步进行四舍五入保留2位小数
return Math.round(result * 100) / 100.0;
}
private void generateWrongOptions(String[] options, Set<String> usedValues,
double answer, boolean isIntegerAnswer) {
for (int i = 1; i < 4; i++) {
String wrongAnswer;
int attempts = 0;
do {
double wrongValue = generateWrongValue(answer, isIntegerAnswer);
wrongAnswer = formatAnswer(wrongValue,
Math.abs(wrongValue - Math.round(wrongValue)) < 1e-10);
attempts++;
} while (usedValues.contains(wrongAnswer) && attempts < 20);
options[i] = wrongAnswer;
usedValues.add(wrongAnswer);
}
}
private double generateWrongValue(double answer, boolean isIntegerAnswer) {
double offset = (random.nextDouble() * 5) + 1;
if (isIntegerAnswer) {
int intAnswer = (int) Math.round(answer);
int intOffset = random.nextInt(10) + 1;
double wrongValue = random.nextBoolean() ?
intAnswer + intOffset : Math.max(1, intAnswer - intOffset);
return wrongValue;
} else {
double wrongValue = random.nextBoolean() ? answer + offset : answer - offset;
// 修改错误选项也保留2位小数
return Math.round(wrongValue * 100) / 100.0;
}
}
// 修改:格式化答案,只在最后一步处理小数位数
private String formatAnswer(double answer, boolean isInteger) {
if (isInteger) {
return String.valueOf((int) Math.round(answer));
} else {
// 确保显示2位小数即使末尾是0
return String.format("%.2f", answer);
}
}
private void shuffleArray(String[] array) {
for (int i = array.length - 1; i > 0; i--) {
int j = random.nextInt(i + 1);
String temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
private double performOperation(double left, double right, int operation) {
switch (operation) {
case 0: return left + right;
case 1: return left - right;
case 2: return left * right;
case 3:
if (right == 0) return Double.NaN;
// 修改:直接进行除法,不在这里保留小数
return left / right;
default: return left;
}
}
protected Question generateOptions(String questionText, double answer, String level) {
String[] options = new String[4];
Set<String> usedValues = new HashSet<>();
// 修改:判断是否为整数的逻辑
boolean isIntegerAnswer = Math.abs(answer - Math.round(answer)) < 1e-10;
String correctAnswer = formatAnswer(answer, isIntegerAnswer);
options[0] = correctAnswer;
usedValues.add(correctAnswer);
generateWrongOptions(options, usedValues, answer, isIntegerAnswer);
shuffleArray(options);
int correctIndex = findCorrectIndex(options, correctAnswer);
return new Question(questionText, options, correctIndex, level);
}
private void generateWrongOptions(String[] options, Set<String> usedValues,
double answer, boolean isIntegerAnswer) {
for (int i = 1; i < 4; i++) {
String wrongAnswer;
int attempts = 0;
do {
double wrongValue = generateWrongValue(answer, isIntegerAnswer);
wrongAnswer = formatAnswer(wrongValue,
Math.abs(wrongValue - Math.round(wrongValue)) < 1e-10);
attempts++;
} while (usedValues.contains(wrongAnswer) && attempts < 20);
options[i] = wrongAnswer;
usedValues.add(wrongAnswer);
}
}
private double generateWrongValue(double answer, boolean isIntegerAnswer) {
double offset = (random.nextDouble() * 5) + 1;
if (isIntegerAnswer) {
int intAnswer = (int) Math.round(answer);
int intOffset = random.nextInt(10) + 1;
double wrongValue = random.nextBoolean() ?
intAnswer + intOffset : Math.max(1, intAnswer - intOffset);
return wrongValue;
} else {
double wrongValue = random.nextBoolean() ? answer + offset : answer - offset;
// 修改错误选项也保留2位小数
return Math.round(wrongValue * 100) / 100.0;
}
}
// 修改:格式化答案,只在最后一步处理小数位数
private String formatAnswer(double answer, boolean isInteger) {
if (isInteger) {
return String.valueOf((int) Math.round(answer));
} else {
// 确保显示2位小数即使末尾是0
return String.format("%.2f", answer);
}
}
private void shuffleArray(String[] array) {
for (int i = array.length - 1; i > 0; i--) {
int j = random.nextInt(i + 1);
String temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
private int findCorrectIndex(String[] options, String correctAnswer) {
for (int i = 0; i < options.length; i++) {
if (options[i].equals(correctAnswer)) return i;
}
return 0;
}
private int findCorrectIndex(String[] options, String correctAnswer) {
for (int i = 0; i < options.length; i++) {
if (options[i].equals(correctAnswer)) return i;
}
return 0;
}
}

@ -1,21 +1,21 @@
package com.mathlearning.model;
public class ConcreteQuestionGenerator extends AbstractQuestionGenerator {
private PrimaryQuestionGenerator primaryGenerator = new PrimaryQuestionGenerator();
private MiddleSchoolQuestionGenerator middleSchoolGenerator = new MiddleSchoolQuestionGenerator();
private HighSchoolQuestionGenerator highSchoolGenerator = new HighSchoolQuestionGenerator();
private PrimaryQuestionGenerator primaryGenerator = new PrimaryQuestionGenerator();
private MiddleSchoolQuestionGenerator middleSchoolGenerator = new MiddleSchoolQuestionGenerator();
private HighSchoolQuestionGenerator highSchoolGenerator = new HighSchoolQuestionGenerator();
@Override
protected Question generateQuestion(String level, int index) {
switch (level) {
case "小学":
return primaryGenerator.generateQuestion(index);
case "初中":
return middleSchoolGenerator.generateQuestion(index);
case "高中":
return highSchoolGenerator.generateQuestion(index);
default:
return primaryGenerator.generateQuestion(index);
@Override
protected Question generateQuestion(String level, int index) {
switch (level) {
case "小学":
return primaryGenerator.generateQuestion(index);
case "初中":
return middleSchoolGenerator.generateQuestion(index);
case "高中":
return highSchoolGenerator.generateQuestion(index);
default:
return primaryGenerator.generateQuestion(index);
}
}
}
}

@ -1,112 +1,112 @@
package com.mathlearning.model;
public class HighSchoolQuestionGenerator extends BaseQuestionGenerator {
private static final int[] SPECIAL_ANGLES = {0, 15, 30, 45, 60, 75, 90};
private static final int[] SPECIAL_ANGLES = {0, 15, 30, 45, 60, 75, 90};
@Override
public Question generateQuestion(int index) {
int operandCount = random.nextInt(5) + 1;
int[] operands = new int[operandCount];
int[] operations = new int[Math.max(0, operandCount - 1)];
boolean[] hasTrigOp = new boolean[operandCount];
int[] trigFunctions = new int[operandCount];
@Override
public Question generateQuestion(int index) {
int operandCount = random.nextInt(5) + 1;
int[] operands = new int[operandCount];
int[] operations = new int[Math.max(0, operandCount - 1)];
boolean[] hasTrigOp = new boolean[operandCount];
int[] trigFunctions = new int[operandCount];
ensureTrigOperation(hasTrigOp, trigFunctions, operandCount);
ensureTrigOperation(hasTrigOp, trigFunctions, operandCount);
String questionText;
double answer;
String questionText;
double answer;
do {
generateOperandsWithTrig(operands, hasTrigOp);
generateOperations(operations, 4);
do {
generateOperandsWithTrig(operands, hasTrigOp);
generateOperations(operations, 4);
questionText = buildQuestionText(operands, operations, hasTrigOp, trigFunctions);
answer = calculateAnswer(operands, operations, hasTrigOp, trigFunctions);
questionText = buildQuestionText(operands, operations, hasTrigOp, trigFunctions);
answer = calculateAnswer(operands, operations, hasTrigOp, trigFunctions);
} while (!isValidAnswer(answer) || Math.abs(answer) > 10000);
} while (!isValidAnswer(answer) || Math.abs(answer) > 10000);
return generateOptions(questionText, answer, "高中");
}
return generateOptions(questionText, answer, "高中");
}
private void ensureTrigOperation(boolean[] hasTrigOp, int[] trigFunctions, int operandCount) {
boolean hasTrigOperation = false;
private void ensureTrigOperation(boolean[] hasTrigOp, int[] trigFunctions, int operandCount) {
boolean hasTrigOperation = false;
for (int i = 0; i < operandCount; i++) {
hasTrigOp[i] = random.nextDouble() < 0.6;
if (hasTrigOp[i]) hasTrigOperation = true;
}
for (int i = 0; i < operandCount; i++) {
hasTrigOp[i] = random.nextDouble() < 0.6;
if (hasTrigOp[i]) hasTrigOperation = true;
}
if (!hasTrigOperation && operandCount > 0) {
int idx = random.nextInt(operandCount);
hasTrigOp[idx] = true;
trigFunctions[idx] = random.nextInt(3);
if (!hasTrigOperation && operandCount > 0) {
int idx = random.nextInt(operandCount);
hasTrigOp[idx] = true;
trigFunctions[idx] = random.nextInt(3);
}
}
}
private void generateOperandsWithTrig(int[] operands, boolean[] hasTrigOp) {
for (int i = 0; i < operands.length; i++) {
operands[i] = hasTrigOp[i] ?
SPECIAL_ANGLES[random.nextInt(SPECIAL_ANGLES.length)] :
random.nextInt(100) + 1;
private void generateOperandsWithTrig(int[] operands, boolean[] hasTrigOp) {
for (int i = 0; i < operands.length; i++) {
operands[i] = hasTrigOp[i] ?
SPECIAL_ANGLES[random.nextInt(SPECIAL_ANGLES.length)] :
random.nextInt(100) + 1;
}
}
}
private String buildQuestionText(int[] operands, int[] operations,
boolean[] hasTrigOp, int[] trigFunctions) {
StringBuilder sb = new StringBuilder();
private String buildQuestionText(int[] operands, int[] operations,
boolean[] hasTrigOp, int[] trigFunctions) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < operands.length; i++) {
if (i > 0) appendOperator(sb, operations[i - 1]);
for (int i = 0; i < operands.length; i++) {
if (i > 0) appendOperator(sb, operations[i - 1]);
if (hasTrigOp[i]) {
appendTrigFunction(sb, operands[i], trigFunctions[i]);
} else {
sb.append(operands[i]);
}
}
if (hasTrigOp[i]) {
appendTrigFunction(sb, operands[i], trigFunctions[i]);
} else {
sb.append(operands[i]);
}
}
sb.append(" = ?");
return sb.toString();
}
sb.append(" = ?");
return sb.toString();
}
private void appendTrigFunction(StringBuilder sb, int operand, int trigFunction) {
switch (trigFunction) {
case 0: sb.append("sin(").append(operand).append("°)"); break;
case 1: sb.append("cos(").append(operand).append("°)"); break;
case 2: sb.append("tan(").append(operand).append("°)"); break;
private void appendTrigFunction(StringBuilder sb, int operand, int trigFunction) {
switch (trigFunction) {
case 0: sb.append("sin(").append(operand).append("°)"); break;
case 1: sb.append("cos(").append(operand).append("°)"); break;
case 2: sb.append("tan(").append(operand).append("°)"); break;
}
}
}
private double calculateAnswer(int[] operands, int[] operations,
boolean[] hasTrigOp, int[] trigFunctions) {
double[] processedValues = processTrigOperations(operands, hasTrigOp, trigFunctions);
return calculateExpression(processedValues, operations);
}
private double[] processTrigOperations(int[] operands, boolean[] hasTrigOp, int[] trigFunctions) {
double[] processedValues = new double[operands.length];
for (int i = 0; i < operands.length; i++) {
if (hasTrigOp[i]) {
processedValues[i] = calculateTrigFunction(operands[i], trigFunctions[i]);
} else {
processedValues[i] = operands[i];
}
private double calculateAnswer(int[] operands, int[] operations,
boolean[] hasTrigOp, int[] trigFunctions) {
double[] processedValues = processTrigOperations(operands, hasTrigOp, trigFunctions);
return calculateExpression(processedValues, operations);
}
return processedValues;
}
private double[] processTrigOperations(int[] operands, boolean[] hasTrigOp, int[] trigFunctions) {
double[] processedValues = new double[operands.length];
private double calculateTrigFunction(int angle, int trigFunction) {
double radians = Math.toRadians(angle);
for (int i = 0; i < operands.length; i++) {
if (hasTrigOp[i]) {
processedValues[i] = calculateTrigFunction(operands[i], trigFunctions[i]);
} else {
processedValues[i] = operands[i];
}
}
return processedValues;
}
switch (trigFunction) {
case 0: return Math.sin(radians);
case 1: return Math.cos(radians);
case 2:
if (angle % 180 == 90 && angle % 360 != 270) return Double.NaN;
return Math.tan(radians);
default: return 0;
private double calculateTrigFunction(int angle, int trigFunction) {
double radians = Math.toRadians(angle);
switch (trigFunction) {
case 0: return Math.sin(radians);
case 1: return Math.cos(radians);
case 2:
if (angle % 180 == 90 && angle % 360 != 270) return Double.NaN;
return Math.tan(radians);
default: return 0;
}
}
}
}

@ -2,90 +2,90 @@ package com.mathlearning.model;
public class MiddleSchoolQuestionGenerator extends BaseQuestionGenerator {
@Override
public Question generateQuestion(int index) {
int operandCount = random.nextInt(5) + 1;
int[] operands = new int[operandCount];
int[] operations = new int[Math.max(0, operandCount - 1)];
boolean[] hasSpecialOp = new boolean[operandCount];
int[] specialOpTypes = new int[operandCount];
@Override
public Question generateQuestion(int index) {
int operandCount = random.nextInt(5) + 1;
int[] operands = new int[operandCount];
int[] operations = new int[Math.max(0, operandCount - 1)];
boolean[] hasSpecialOp = new boolean[operandCount];
int[] specialOpTypes = new int[operandCount];
ensureSpecialOperation(hasSpecialOp, specialOpTypes, operandCount);
ensureSpecialOperation(hasSpecialOp, specialOpTypes, operandCount);
String questionText;
double answer;
String questionText;
double answer;
do {
generateOperands(operands, 1, 100);
generateOperations(operations, 4);
do {
generateOperands(operands, 1, 100);
generateOperations(operations, 4);
questionText = buildQuestionText(operands, operations, hasSpecialOp, specialOpTypes);
answer = calculateAnswer(operands, operations, hasSpecialOp, specialOpTypes);
questionText = buildQuestionText(operands, operations, hasSpecialOp, specialOpTypes);
answer = calculateAnswer(operands, operations, hasSpecialOp, specialOpTypes);
} while (!isValidAnswer(answer) || Math.abs(answer) > 10000);
} while (!isValidAnswer(answer) || Math.abs(answer) > 10000);
return generateOptions(questionText, answer, "初中");
}
return generateOptions(questionText, answer, "初中");
}
private void ensureSpecialOperation(boolean[] hasSpecialOp, int[] specialOpTypes, int operandCount) {
boolean hasSpecialOperation = false;
private void ensureSpecialOperation(boolean[] hasSpecialOp, int[] specialOpTypes, int operandCount) {
boolean hasSpecialOperation = false;
for (int i = 0; i < operandCount; i++) {
hasSpecialOp[i] = random.nextDouble() < 0.6;
if (hasSpecialOp[i]) hasSpecialOperation = true;
}
for (int i = 0; i < operandCount; i++) {
hasSpecialOp[i] = random.nextDouble() < 0.6;
if (hasSpecialOp[i]) hasSpecialOperation = true;
}
if (!hasSpecialOperation && operandCount > 0) {
int idx = random.nextInt(operandCount);
hasSpecialOp[idx] = true;
specialOpTypes[idx] = random.nextInt(2);
if (!hasSpecialOperation && operandCount > 0) {
int idx = random.nextInt(operandCount);
hasSpecialOp[idx] = true;
specialOpTypes[idx] = random.nextInt(2);
}
}
}
private String buildQuestionText(int[] operands, int[] operations,
boolean[] hasSpecialOp, int[] specialOpTypes) {
StringBuilder sb = new StringBuilder();
private String buildQuestionText(int[] operands, int[] operations,
boolean[] hasSpecialOp, int[] specialOpTypes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < operands.length; i++) {
if (i > 0) appendOperator(sb, operations[i - 1]);
for (int i = 0; i < operands.length; i++) {
if (i > 0) appendOperator(sb, operations[i - 1]);
if (hasSpecialOp[i]) {
appendSpecialOperation(sb, operands[i], specialOpTypes[i]);
} else {
sb.append(operands[i]);
}
}
if (hasSpecialOp[i]) {
appendSpecialOperation(sb, operands[i], specialOpTypes[i]);
} else {
sb.append(operands[i]);
}
}
sb.append(" = ?");
return sb.toString();
}
sb.append(" = ?");
return sb.toString();
}
private void appendSpecialOperation(StringBuilder sb, int operand, int opType) {
if (opType == 0) {
sb.append(operand).append("²");
} else {
sb.append("√").append(operand);
private void appendSpecialOperation(StringBuilder sb, int operand, int opType) {
if (opType == 0) {
sb.append(operand).append("²");
} else {
sb.append("√").append(operand);
}
}
}
private double calculateAnswer(int[] operands, int[] operations,
boolean[] hasSpecialOp, int[] specialOpTypes) {
double[] processedValues = processSpecialOperations(operands, hasSpecialOp, specialOpTypes);
return calculateExpression(processedValues, operations);
}
private double[] processSpecialOperations(int[] operands, boolean[] hasSpecialOp, int[] specialOpTypes) {
double[] processedValues = new double[operands.length];
for (int i = 0; i < operands.length; i++) {
if (hasSpecialOp[i]) {
processedValues[i] = (specialOpTypes[i] == 0) ?
operands[i] * operands[i] : Math.sqrt(operands[i]);
} else {
processedValues[i] = operands[i];
}
private double calculateAnswer(int[] operands, int[] operations,
boolean[] hasSpecialOp, int[] specialOpTypes) {
double[] processedValues = processSpecialOperations(operands, hasSpecialOp, specialOpTypes);
return calculateExpression(processedValues, operations);
}
return processedValues;
}
private double[] processSpecialOperations(int[] operands, boolean[] hasSpecialOp, int[] specialOpTypes) {
double[] processedValues = new double[operands.length];
for (int i = 0; i < operands.length; i++) {
if (hasSpecialOp[i]) {
processedValues[i] = (specialOpTypes[i] == 0) ?
operands[i] * operands[i] : Math.sqrt(operands[i]);
} else {
processedValues[i] = operands[i];
}
}
return processedValues;
}
}

@ -5,139 +5,139 @@ import java.util.List;
public class PrimaryQuestionGenerator extends BaseQuestionGenerator {
@Override
public Question generateQuestion(int index) {
int operandCount = random.nextInt(4) + 2;
int[] operands = new int[operandCount];
int[] operations = new int[operandCount - 1];
String questionText;
double answer;
do {
generateOperands(operands, 1, 100);
generateOperations(operations, 4);
boolean useParentheses = random.nextDouble() < 0.3 && operandCount >= 3;
int[] parenthesesPositions = findParenthesesPositions(operations, useParentheses);
questionText = buildQuestionText(operands, operations,
parenthesesPositions[0], parenthesesPositions[1]);
answer = calculateAnswer(operands, operations,
parenthesesPositions[0], parenthesesPositions[1]);
} while (!isValidAnswer(answer));
return generateOptions(questionText, answer, "小学");
}
private int[] findParenthesesPositions(int[] operations, boolean useParentheses) {
int parenStart = -1;
int parenEnd = -1;
if (useParentheses) {
parenStart = findSuitableParenthesesPosition(operations);
if (parenStart != -1) {
parenEnd = parenStart + 1;
if (parenEnd < operations.length && random.nextBoolean()) {
parenEnd++;
}
}
}
@Override
public Question generateQuestion(int index) {
int operandCount = random.nextInt(4) + 2;
int[] operands = new int[operandCount];
int[] operations = new int[operandCount - 1];
String questionText;
double answer;
return new int[]{parenStart, parenEnd};
}
do {
generateOperands(operands, 1, 100);
generateOperations(operations, 4);
private int findSuitableParenthesesPosition(int[] operations) {
List<Integer> suitablePositions = new ArrayList<>();
boolean useParentheses = random.nextDouble() < 0.3 && operandCount >= 3;
int[] parenthesesPositions = findParenthesesPositions(operations, useParentheses);
for (int i = 0; i < operations.length; i++) {
if ((operations[i] == 0 || operations[i] == 1) && i > 0) {
suitablePositions.add(i);
}
questionText = buildQuestionText(operands, operations,
parenthesesPositions[0], parenthesesPositions[1]);
answer = calculateAnswer(operands, operations,
parenthesesPositions[0], parenthesesPositions[1]);
} while (!isValidAnswer(answer));
return generateOptions(questionText, answer, "小学");
}
if (suitablePositions.isEmpty()) {
return operations.length > 1 ? random.nextInt(operations.length - 1) : -1;
private int[] findParenthesesPositions(int[] operations, boolean useParentheses) {
int parenStart = -1;
int parenEnd = -1;
if (useParentheses) {
parenStart = findSuitableParenthesesPosition(operations);
if (parenStart != -1) {
parenEnd = parenStart + 1;
if (parenEnd < operations.length && random.nextBoolean()) {
parenEnd++;
}
}
}
return new int[]{parenStart, parenEnd};
}
return suitablePositions.get(random.nextInt(suitablePositions.size()));
}
private int findSuitableParenthesesPosition(int[] operations) {
List<Integer> suitablePositions = new ArrayList<>();
private String buildQuestionText(int[] operands, int[] operations, int parenStart, int parenEnd) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < operations.length; i++) {
if ((operations[i] == 0 || operations[i] == 1) && i > 0) {
suitablePositions.add(i);
}
}
for (int i = 0; i < operands.length; i++) {
if (i == parenStart) sb.append("(");
sb.append(operands[i]);
if (i == parenEnd) sb.append(")");
if (i < operations.length) appendOperator(sb, operations[i]);
if (suitablePositions.isEmpty()) {
return operations.length > 1 ? random.nextInt(operations.length - 1) : -1;
}
return suitablePositions.get(random.nextInt(suitablePositions.size()));
}
sb.append(" = ?");
return sb.toString();
}
private String buildQuestionText(int[] operands, int[] operations, int parenStart, int parenEnd) {
StringBuilder sb = new StringBuilder();
private double calculateAnswer(int[] operands, int[] operations, int parenStart, int parenEnd) {
if (parenStart == -1 || parenEnd == -1) {
return calculateWithoutParentheses(operands, operations);
for (int i = 0; i < operands.length; i++) {
if (i == parenStart) sb.append("(");
sb.append(operands[i]);
if (i == parenEnd) sb.append(")");
if (i < operations.length) appendOperator(sb, operations[i]);
}
sb.append(" = ?");
return sb.toString();
}
return calculateWithParentheses(operands, operations, parenStart, parenEnd);
}
private double calculateAnswer(int[] operands, int[] operations, int parenStart, int parenEnd) {
if (parenStart == -1 || parenEnd == -1) {
return calculateWithoutParentheses(operands, operations);
}
private double calculateWithParentheses(int[] operands, int[] operations, int parenStart, int parenEnd) {
// 修改使用Double列表
List<Double> numbers = new ArrayList<>();
List<Integer> ops = new ArrayList<>();
return calculateWithParentheses(operands, operations, parenStart, parenEnd);
}
for (int operand : operands) numbers.add((double) operand);
for (int operation : operations) ops.add(operation);
private double calculateWithParentheses(int[] operands, int[] operations, int parenStart, int parenEnd) {
// 修改使用Double列表
List<Double> numbers = new ArrayList<>();
List<Integer> ops = new ArrayList<>();
double parenthesesResult = calculateParenthesesContent(numbers, ops, parenStart, parenEnd);
for (int operand : operands) numbers.add((double) operand);
for (int operation : operations) ops.add(operation);
if (!isValidAnswer(parenthesesResult)) return -1;
double parenthesesResult = calculateParenthesesContent(numbers, ops, parenStart, parenEnd);
replaceWithParenthesesResult(numbers, ops, parenStart, parenEnd, parenthesesResult);
// 修改:调用新的方法名
return calculateDoubleList(numbers, ops);
}
if (!isValidAnswer(parenthesesResult)) return -1;
private double calculateParenthesesContent(List<Double> numbers, List<Integer> ops, int start, int end) {
List<Double> parenNumbers = new ArrayList<>();
List<Integer> parenOps = new ArrayList<>();
replaceWithParenthesesResult(numbers, ops, parenStart, parenEnd, parenthesesResult);
// 修改:调用新的方法名
return calculateDoubleList(numbers, ops);
}
for (int i = start; i <= end; i++) parenNumbers.add(numbers.get(i));
for (int i = start; i < end; i++) parenOps.add(ops.get(i));
private double calculateParenthesesContent(List<Double> numbers, List<Integer> ops, int start, int end) {
List<Double> parenNumbers = new ArrayList<>();
List<Integer> parenOps = new ArrayList<>();
// 修改:调用新的方法名
return calculateDoubleList(parenNumbers, parenOps);
}
for (int i = start; i <= end; i++) parenNumbers.add(numbers.get(i));
for (int i = start; i < end; i++) parenOps.add(ops.get(i));
private void replaceWithParenthesesResult(List<Double> numbers, List<Integer> ops,
int start, int end, double result) {
int numCountToRemove = end - start + 1;
int opCountToRemove = end - start;
// 修改:调用新的方法名
return calculateDoubleList(parenNumbers, parenOps);
}
for (int i = 0; i < numCountToRemove; i++) numbers.remove(start);
for (int i = 0; i < opCountToRemove; i++) ops.remove(start);
private void replaceWithParenthesesResult(List<Double> numbers, List<Integer> ops,
int start, int end, double result) {
int numCountToRemove = end - start + 1;
int opCountToRemove = end - start;
// 修改直接添加double结果不转换为int
numbers.add(start, result);
}
for (int i = 0; i < numCountToRemove; i++) numbers.remove(start);
for (int i = 0; i < opCountToRemove; i++) ops.remove(start);
// 修改:重命名方法,避免与父类冲突
private double calculateDoubleList(List<Double> numbers, List<Integer> ops) {
double[] doubleNumbers = new double[numbers.size()];
for (int i = 0; i < numbers.size(); i++) {
doubleNumbers[i] = numbers.get(i);
// 修改直接添加double结果不转换为int
numbers.add(start, result);
}
int[] intOps = new int[ops.size()];
for (int i = 0; i < ops.size(); i++) {
intOps[i] = ops.get(i);
}
// 修改:重命名方法,避免与父类冲突
private double calculateDoubleList(List<Double> numbers, List<Integer> ops) {
double[] doubleNumbers = new double[numbers.size()];
for (int i = 0; i < numbers.size(); i++) {
doubleNumbers[i] = numbers.get(i);
}
return calculateExpression(doubleNumbers, intOps);
}
int[] intOps = new int[ops.size()];
for (int i = 0; i < ops.size(); i++) {
intOps[i] = ops.get(i);
}
return calculateExpression(doubleNumbers, intOps);
}
}

@ -1,7 +1,6 @@
package com.mathlearning.model;
public class User {
private String user_name; // 从第一个版本保留
private String email;
private String password;
private String registrationCode;
@ -12,7 +11,6 @@ public class User {
this.registrationCode = registrationCode;
this.isRegistered = false;
this.password = null;
this.user_name = null; // 添加初始化
}
// Getters and setters
@ -24,15 +22,6 @@ public class User {
this.email = email;
}
// user_name 相关方法(从第一个版本保留)
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getPassword() {
return password;
}
@ -56,4 +45,4 @@ public class User {
public void setRegistered(boolean registered) {
isRegistered = registered;
}
}
}

@ -60,10 +60,7 @@ public class AuthService {
}
public boolean validatePassword(String password) {
if (password == null
|| password.length() < 6
|| password.length() > 10
|| password.contains("|")) {
if (password == null || password.length() < 6 || password.length() > 10) {
return false;
}

@ -27,11 +27,10 @@ public class UserService {
return true;
}
public boolean completeRegistration(String email, String user_name, String password) {
public boolean completeRegistration(String email, String password) {
for (User user : users) {
if (user.getEmail().equals(email) && !user.isRegistered()) {
user.setPassword(password);
user.setUser_name(user_name);
user.setRegistered(true);
saveUsers();
return true;
@ -71,15 +70,6 @@ public class UserService {
return null;
}
public User getUserByUsername(String username) {
for (User user : users) {
if (user.getUser_name() != null && user.getUser_name().equals(username)) {
return user;
}
}
return null;
}
public boolean updateUser(User updatedUser) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getEmail().equals(updatedUser.getEmail())) {
@ -103,11 +93,10 @@ public class UserService {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\\|");
if (parts.length >= 5) {
User user = new User(parts[0], parts[2]);
user.setUser_name(parts[1]);
user.setPassword(parts[3]);
user.setRegistered(Boolean.parseBoolean(parts[4]));
if (parts.length >= 4) {
User user = new User(parts[0], parts[1]);
user.setPassword(parts[2]);
user.setRegistered(Boolean.parseBoolean(parts[3]));
userList.add(user);
}
}
@ -127,8 +116,6 @@ public class UserService {
for (User user : users) {
String line =
user.getEmail()
+ "|"
+ user.getUser_name()
+ "|"
+ user.getRegistrationCode()
+ "|"

@ -75,7 +75,7 @@ public class LevelSelectionFrame extends JFrame {
// 底部显示当前用户信息
if (authController.getCurrentUser() != null) {
JLabel userInfoLabel =
new JLabel("当前用户: " + authController.getCurrentUser().getUser_name(), JLabel.CENTER);
new JLabel("当前用户: " + authController.getCurrentUser().getEmail(), JLabel.CENTER);
userInfoLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
userInfoLabel.setForeground(Color.GRAY);
userInfoLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));

@ -28,7 +28,7 @@ public class PasswordChangeFrame extends JFrame {
panel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
// 显示当前用户信息
JLabel userLabel = new JLabel("当前用户: " + authController.getCurrentUserName());
JLabel userLabel = new JLabel("当前用户: " + authController.getCurrentUserEmail());
userLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 12));
userLabel.setForeground(new Color(0, 100, 200));
@ -80,11 +80,7 @@ public class PasswordChangeFrame extends JFrame {
} else {
JOptionPane.showMessageDialog(
this,
"密码修改失败!\n"
+ "可能的原因:\n"
+ "• 原密码不正确\n"
+ "• 新密码不符合要求\n"
+ "• 密码要求6-10位包含大小写字母和数字且不包含非法字符|\n",
"密码修改失败!\n" + "可能的原因:\n" + "• 原密码不正确\n" + "• 新密码不符合要求6-10位包含大小写字母和数字",
"错误",
JOptionPane.ERROR_MESSAGE);
}

@ -12,7 +12,6 @@ public class PasswordSetupFrame extends JFrame {
private AuthController authController;
private JPasswordField passwordField;
private JPasswordField confirmPasswordField;
private JTextField usernameField;
private JLabel resultLabel;
public PasswordSetupFrame(String email, String code, AuthController authController) {
@ -25,94 +24,59 @@ public class PasswordSetupFrame extends JFrame {
private void initializeUI() {
setTitle("设置密码");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(350, 400);
setSize(450, 300);
setLocationRelativeTo(null);
setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(null);
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// 添加顶部返回按钮
JPanel topPanel = new JPanel(new BorderLayout());
JButton backButton = new JButton("← 返回注册");
backButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
backButton.setBackground(new Color(200, 200, 200));
backButton.addActionListener(e -> backToRegister());
backButton.setBounds(20, 20, 100, 25);
panel.add(backButton);
topPanel.add(backButton, BorderLayout.WEST);
JLabel titleLabel = new JLabel("用户:" + email);
mainPanel.add(topPanel, BorderLayout.NORTH);
JLabel titleLabel = new JLabel("用户:" + email, JLabel.CENTER);
titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
titleLabel.setBounds(20, 50, 300, 20);
panel.add(titleLabel);
mainPanel.add(titleLabel, BorderLayout.CENTER);
JLabel usernameLabel = new JLabel("用户名:");
usernameLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
usernameField = new JTextField();
usernameLabel.setBounds(20, 90, 100, 30);
usernameField.setBounds(100, 90, 200, 30);
JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 10));
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
passwordField = new JPasswordField();
passwordLabel.setBounds(20, 150, 50, 30);
passwordField.setBounds(100, 150, 200, 30);
JLabel confirmLabel = new JLabel("确认密码:");
confirmLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
confirmPasswordField = new JPasswordField();
confirmLabel.setBounds(20, 210, 80, 30);
confirmPasswordField.setBounds(100, 210, 200, 30);
JButton submitButton = new JButton("完成");
submitButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
submitButton.setBounds(110, 290, 80, 30);
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(confirmLabel);
panel.add(confirmPasswordField);
panel.add(new JLabel());
panel.add(submitButton);
resultLabel = new JLabel("用户名需1-8位密码需6-10位包含大小写字母和数字");
formPanel.add(passwordLabel);
formPanel.add(passwordField);
formPanel.add(confirmLabel);
formPanel.add(confirmPasswordField);
formPanel.add(new JLabel());
formPanel.add(submitButton);
resultLabel = new JLabel("密码需6-10位包含大小写字母和数字", JLabel.CENTER);
resultLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
resultLabel.setForeground(Color.BLUE);
resultLabel.setBounds(20, 240, 300, 30);
panel.add(resultLabel);
submitButton.addActionListener(new SubmitAction());
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(formPanel, BorderLayout.CENTER);
centerPanel.add(resultLabel, BorderLayout.SOUTH);
mainPanel.add(centerPanel, BorderLayout.SOUTH);
add(panel);
}
submitButton.addActionListener(new SubmitAction());
private boolean examingTextField(String password, String confirmPassword, String username) {
if (password.isEmpty() || confirmPassword.isEmpty()) {
resultLabel.setText("请填写所有字段");
resultLabel.setForeground(Color.RED);
return false;
}
if (username.length() > 8) {
resultLabel.setText("用户名超过8个字符");
resultLabel.setForeground(Color.RED);
return false;
}
if (username.contains("|") || password.contains("|")) {
resultLabel.setText("用户名或密码包含非法字符|");
resultLabel.setForeground(Color.RED);
return false;
}
if (!authController.isUsernameAvailable(username)) {
resultLabel.setText("该用户名已存在");
resultLabel.setForeground(Color.RED);
return false;
}
if (!password.equals(confirmPassword)) {
resultLabel.setText("两次输入的密码不一致");
resultLabel.setForeground(Color.RED);
return false;
}
return true;
add(mainPanel);
}
private class SubmitAction implements ActionListener {
@ -120,20 +84,34 @@ public class PasswordSetupFrame extends JFrame {
public void actionPerformed(ActionEvent e) {
String password = new String(passwordField.getPassword());
String confirmPassword = new String(confirmPasswordField.getPassword());
String username = new String(usernameField.getText());
if (!examingTextField(password, confirmPassword, username)) {
if (password.isEmpty() || confirmPassword.isEmpty()) {
resultLabel.setText("请填写所有字段");
resultLabel.setForeground(Color.RED);
return;
}
if (!password.equals(confirmPassword)) {
resultLabel.setText("两次输入的密码不一致");
resultLabel.setForeground(Color.RED);
return;
}
if (authController.completeRegistration(email, username, password)) {
// 使用已验证的验证码完成注册
if (authController.completeRegistration(email, code, password)) {
JOptionPane.showMessageDialog(
PasswordSetupFrame.this, "注册成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
// 自动登录并跳转到难度选择界面
if (authController.login(email, password)) {
LevelSelectionFrame levelFrame = new LevelSelectionFrame(authController);
levelFrame.setVisible(true);
dispose();
} else {
// 如果自动登录失败,跳转到登录界面
LoginFrame loginFrame = new LoginFrame();
loginFrame.setVisible(true);
dispose();
}
} else {

@ -56,7 +56,6 @@ public class QuestionCountFrame extends JFrame {
@Override
public void windowClosing(WindowEvent e) {
parentFrame.setVisible(true);
setVisible(false);
}
});
add(mainPanel);

@ -94,7 +94,7 @@ public class QuizFrame extends JFrame {
// 选项面板
JPanel optionsPanel = new JPanel(new GridLayout(4, 1, 10, 10));
optionsPanel.setBorder(
BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "请选择答案"));
BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "请选择答案"));
optionButtons = new JRadioButton[4];
buttonGroup = new ButtonGroup();
@ -108,11 +108,11 @@ public class QuizFrame extends JFrame {
final int index = i;
optionButtons[i].addActionListener(
e -> {
// 保存当前选择
userSelections[currentDisplayIndex] = index;
System.out.println("用户选择: 第" + (currentDisplayIndex + 1) + "题 -> 选项" + index);
});
e -> {
// 保存当前选择
userSelections[currentDisplayIndex] = index;
System.out.println("用户选择: 第" + (currentDisplayIndex + 1) + "题 -> 选项" + index);
});
buttonGroup.add(optionButtons[i]);
optionsPanel.add(optionButtons[i]);
@ -161,16 +161,16 @@ public class QuizFrame extends JFrame {
}
Question currentQuestion = allQuestions.get(currentDisplayIndex);
questionNumberLabel.setText(
String.format("第 %d 题 / 共 %d 题", currentDisplayIndex + 1, totalQuestions));
String.format("第 %d 题 / 共 %d 题", currentDisplayIndex + 1, totalQuestions));
questionTextLabel.setText(
"<html><div style='text-align: center;'>"
+ currentQuestion.getQuestionText()
+ "</div></html>");
"<html><div style='text-align: center;'>"
+ currentQuestion.getQuestionText()
+ "</div></html>");
String[] options = currentQuestion.getOptions();
for (int i = 0; i < 4; i++) {
String optionText =
String.format(
"<html><div style='padding: 5px;'>%c. %s</div></html>", (char) ('A' + i), options[i]);
String.format(
"<html><div style='padding: 5px;'>%c. %s</div></html>", (char) ('A' + i), options[i]);
optionButtons[i].setText(optionText);
}
buttonGroup.clearSelection(); // 清除当前选择状态
@ -184,17 +184,17 @@ public class QuizFrame extends JFrame {
// 更新进度条
progressBar.setValue(currentDisplayIndex);
progressBar.setString(
String.format(
"%d/%d (%.0f%%)",
currentDisplayIndex,
totalQuestions,
((double) currentDisplayIndex / totalQuestions) * 100));
String.format(
"%d/%d (%.0f%%)",
currentDisplayIndex,
totalQuestions,
((double) currentDisplayIndex / totalQuestions) * 100));
updateButtonStates(); // 更新按钮状态
}
private void exitQuiz() {
int result =
JOptionPane.showConfirmDialog(this, "确定要退出答题吗?所有进度将丢失。", "退出确认", JOptionPane.YES_NO_OPTION);
JOptionPane.showConfirmDialog(this, "确定要退出答题吗?所有进度将丢失。", "退出确认", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
parentFrame.setVisible(true);
this.dispose();
@ -238,8 +238,8 @@ public class QuizFrame extends JFrame {
// 如果没有选择答案,提示用户但允许继续(除了最后一题)
if (selectedIndex == -1) {
int result =
JOptionPane.showConfirmDialog(
QuizFrame.this, "您还没有选择答案,确定要继续下一题吗?", "确认", JOptionPane.YES_NO_OPTION);
JOptionPane.showConfirmDialog(
QuizFrame.this, "您还没有选择答案,确定要继续下一题吗?", "确认", JOptionPane.YES_NO_OPTION);
if (result != JOptionPane.YES_OPTION) {
return;
}
@ -274,9 +274,8 @@ public class QuizFrame extends JFrame {
private void finishQuiz() {
// 使用原有的构造函数
ScoreFrame scoreFrame =
new ScoreFrame(questionController, userSelections, allQuestions, parentFrame);
ScoreFrame scoreFrame = new ScoreFrame(questionController, userSelections, allQuestions, parentFrame);
scoreFrame.setVisible(true);
this.dispose();
}
} // 添加这个闭合大括号
} // 添加这个闭合大括号

@ -13,10 +13,10 @@ public class ScoreFrame extends JFrame {
private LevelSelectionFrame parentFrame;
public ScoreFrame(
QuestionController questionController,
int[] userSelections,
List<Question> allQuestions,
LevelSelectionFrame parent) {
QuestionController questionController,
int[] userSelections,
List<Question> allQuestions,
LevelSelectionFrame parent) {
this.parentFrame = parent;
this.questionController = questionController;
this.userSelections = userSelections;
@ -40,7 +40,7 @@ public class ScoreFrame extends JFrame {
double percentage = (double) score / total * 100; // 手动计算百分比
JLabel scoreLabel =
new JLabel(String.format("得分: %d/%d (%.1f%%)", score, total, percentage), JLabel.CENTER);
new JLabel(String.format("得分: %d/%d (%.1f%%)", score, total, percentage), JLabel.CENTER);
scoreLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 24));
Color color;
@ -67,15 +67,17 @@ public class ScoreFrame extends JFrame {
details.append(String.format("第%d题: %s\n", i + 1, q.getQuestionText()));
details.append(
String.format(
"你的答案: %s\n",
userAnswer == -1
? "未作答"
: (char) ('A' + userAnswer) + ". " + q.getOptions()[userAnswer]));
String.format(
"你的答案: %s\n",
userAnswer == -1
? "未作答"
: (char) ('A' + userAnswer) + ". " + q.getOptions()[userAnswer]));
details.append(
String.format(
"正确答案: %s\n",
(char) ('A' + q.getCorrectIndex()) + ". " + q.getOptions()[q.getCorrectIndex()]));
String.format(
"正确答案: %s\n",
(char) ('A' + q.getCorrectIndex())
+ ". "
+ q.getOptions()[q.getCorrectIndex()]));
details.append(isCorrect ? "✓ 正确\n" : "✗ 错误\n");
details.append("--------------------\n");
}
@ -107,4 +109,4 @@ public class ScoreFrame extends JFrame {
parentFrame.setVisible(true);
this.dispose();
}
}
}
Loading…
Cancel
Save