From 29e1d31de73c36d529ef67da5770bdcdde74f6a3 Mon Sep 17 00:00:00 2001 From: hnu202326010302 <1677625723@qq.com> Date: Sun, 28 Sep 2025 22:41:55 +0800 Subject: [PATCH] ADD file via upload --- src/ProblemManager.java | 141 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/ProblemManager.java diff --git a/src/ProblemManager.java b/src/ProblemManager.java new file mode 100644 index 0000000..b4aabd6 --- /dev/null +++ b/src/ProblemManager.java @@ -0,0 +1,141 @@ +// problem.java +import java.util.Scanner; +import java.io.IOException; + +/** + * 问题管理类,处理用户交互和题目生成流程。 + */ +public class ProblemManager { + private String currentLevel; + private String currentUserName; + + public void display() { + try { + new ProcessBuilder("clear").inheritIO().start().waitFor(); + } catch (IOException | InterruptedException exception) { + // 清屏失败,继续执行 + } + System.out.println("=== 中小学数学卷子自动生成程序 ==="); + } + + public void start(User user) { + currentLevel = user.getLevel(); + currentUserName = user.getUsername(); + + while (true) { + display(); + System.out.println("欢迎你 " + user.getUsername() + " " + currentLevel); + System.out.println("输入1开始出题"); + System.out.println("输入2切换出题年级"); + System.out.println("输入-1退出登录,返回开始界面"); + + Scanner scanner = new Scanner(System.in); + int choice = 0; + + while (true) { + String input = scanner.next(); + try { + choice = Integer.parseInt(input); + } catch (NumberFormatException exception) { + choice = 0; + } + if (choice == 1 || choice == 2 || choice == -1) { + break; + } else { + System.out.println("请输入正确的操作"); + } + } + + if (choice == 1) { + if (generateProblems() == 0) { + continue; + } + } + if (choice == 2) { + if (switchLevel() == 0) { + continue; + } + } + if (choice == -1) { + break; + } + } + } + + public int generateProblems() { + System.out.println("正在为" + currentLevel + "出题"); + System.out.println("请在10-30的范围内输入出题数目,输入-1返回操作界面"); + + Scanner scanner = new Scanner(System.in); + int problemCount = 0; + + while (true) { + String input = scanner.next(); + try { + problemCount = Integer.parseInt(input); + } catch (NumberFormatException exception) { + problemCount = 0; + } + if (problemCount == -1) { + break; + } + if (problemCount >= 10 && problemCount <= 30) { + break; + } else { + System.out.println("请输入10-30范围的出题数"); + } + } + + if (problemCount == -1) { + return 0; + } + + if (currentLevel.equals("小学")) { + PrimarySchoolProblemGenerator primaryGenerator = new PrimarySchoolProblemGenerator(); + primaryGenerator.start(problemCount, currentUserName); + } else if (currentLevel.equals("初中")) { + MiddleSchoolProblemGenerator middleGenerator = new MiddleSchoolProblemGenerator(); + middleGenerator.start(problemCount, currentUserName); + } else if (currentLevel.equals("高中")) { + HighSchoolProblemGenerator highGenerator = new HighSchoolProblemGenerator(); + highGenerator.start(problemCount, currentUserName); + } + + System.out.println("已出题完成,输入-1返回操作界面"); + while (!scanner.next().equals("-1")) { + continue; + } + return 0; + } + + public int switchLevel() { + System.out.println("请输入想要切换的出题年级,输入-1返回操作界面"); + + Scanner scanner = new Scanner(System.in); + String newLevel; + + while (true) { + newLevel = scanner.next(); + if (newLevel.equals("-1")) { + break; + } + if (newLevel.equals("初中") || newLevel.equals("小学") || newLevel.equals("高中")) { + break; + } else { + System.out.println("请输入小学、初中和高中三个选项中的一个"); + } + } + + if (newLevel.equals("-1")) { + return 0; + } + + currentLevel = newLevel; + System.out.println("已成功修改出题年级为" + currentLevel + ",输入-1返回操作界面"); + + while (!scanner.next().equals("-1")) { + continue; + } + return 0; + } +} \ No newline at end of file