You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
math_question/controller/ExamController.java

59 lines
1.9 KiB

package controller;
import service.QuestionService;
import view.ExamFrame;
import java.util.List;
import javax.swing.JOptionPane;
public class ExamController {
private MainController mainController;
private QuestionService questionService = new QuestionService();
public ExamController(MainController mainController) {
this.mainController = mainController;
}
public void startExam(String difficulty) {
try {
String input = JOptionPane.showInputDialog("请输入题目数量:");
if (input != null) {
int count = Integer.parseInt(input);
if (count > 0) {
List<model.Question> questions = questionService.generateQuestions(difficulty, count);
new ExamFrame(this, questions);
} else {
JOptionPane.showMessageDialog(null, "题目数量必须大于0");
}
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "请输入有效数字");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "生成题目时出现错误: " + e.getMessage());
}
}
public void showResult(int score, int total) {
// 计算百分比
int percentage = (int) ((double) score / total * 100);
// 显示成绩
JOptionPane.showMessageDialog(null,
"考试结束!\n" +
"得分: " + score + "/" + total + "\n" +
"正确率: " + percentage + "%",
"考试结果",
JOptionPane.INFORMATION_MESSAGE);
// 重新显示主界面
mainController.showMainFrame();
}
public void returnToMain() {
// 返回到主界面
mainController.showMainFrame();
}
// 获取主控制器(如果需要)
public MainController getMainController() {
return mainController;
}
}