|
|
|
|
@ -143,16 +143,12 @@ public class MathQuestionGenerator {
|
|
|
|
|
showOperationMenu(currentGrade);
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.print("请输入操作:");
|
|
|
|
|
String input = scanner.nextLine().trim();
|
|
|
|
|
|
|
|
|
|
if (input.equals("-1")) {
|
|
|
|
|
currentUser = null;
|
|
|
|
|
System.out.println("退出当前用户,重新登录...");
|
|
|
|
|
String input = getUserInput();
|
|
|
|
|
|
|
|
|
|
if (handleExitCommand(input)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (input.startsWith("切换为")) {
|
|
|
|
|
if (handleSwitchCommand(input)) {
|
|
|
|
|
currentGrade = currentUser.getGrade();
|
|
|
|
|
@ -160,19 +156,51 @@ public class MathQuestionGenerator {
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
int count = Integer.parseInt(input);
|
|
|
|
|
if (count < MIN_QUESTION_COUNT || count > MAX_QUESTION_COUNT) {
|
|
|
|
|
System.out.println("题目数量应在" + MIN_QUESTION_COUNT + "-" + MAX_QUESTION_COUNT + "之间");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateQuestions(count, currentGrade);
|
|
|
|
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
System.out.println("请输入有效的数字(" + MIN_QUESTION_COUNT + "-" + MAX_QUESTION_COUNT + ")或切换命令");
|
|
|
|
|
|
|
|
|
|
handleQuestionGeneration(input, currentGrade);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户输入
|
|
|
|
|
* @return 用户输入
|
|
|
|
|
*/
|
|
|
|
|
private static String getUserInput() {
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.print("请输入操作:");
|
|
|
|
|
return scanner.nextLine().trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理退出命令
|
|
|
|
|
* @param input 用户输入
|
|
|
|
|
* @return 是否退出
|
|
|
|
|
*/
|
|
|
|
|
private static boolean handleExitCommand(String input) {
|
|
|
|
|
if (input.equals("-1")) {
|
|
|
|
|
currentUser = null;
|
|
|
|
|
System.out.println("退出当前用户,重新登录...");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理题目生成
|
|
|
|
|
* @param input 用户输入
|
|
|
|
|
* @param currentGrade 当前年级
|
|
|
|
|
*/
|
|
|
|
|
private static void handleQuestionGeneration(String input, String currentGrade) {
|
|
|
|
|
try {
|
|
|
|
|
int count = Integer.parseInt(input);
|
|
|
|
|
if (count < MIN_QUESTION_COUNT || count > MAX_QUESTION_COUNT) {
|
|
|
|
|
System.out.println("题目数量应在" + MIN_QUESTION_COUNT + "-" + MAX_QUESTION_COUNT + "之间");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
generateQuestions(count, currentGrade);
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
System.out.println("请输入有效的数字(" + MIN_QUESTION_COUNT + "-" + MAX_QUESTION_COUNT + ")或切换命令");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -330,10 +358,28 @@ public class MathQuestionGenerator {
|
|
|
|
|
* @param grade 年级类型
|
|
|
|
|
*/
|
|
|
|
|
private static void saveQuestionsToFile(List<String> questions, File userDir, int count, String grade) {
|
|
|
|
|
File outputFile = createOutputFile(userDir);
|
|
|
|
|
writeQuestionsToFile(questions, outputFile);
|
|
|
|
|
showGenerationResult(count, grade, outputFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建输出文件
|
|
|
|
|
* @param userDir 用户文件夹
|
|
|
|
|
* @return 输出文件
|
|
|
|
|
*/
|
|
|
|
|
private static File createOutputFile(File userDir) {
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
|
|
|
|
|
String filename = dateFormat.format(new Date()) + ".txt";
|
|
|
|
|
File outputFile = new File(userDir, filename);
|
|
|
|
|
|
|
|
|
|
return new File(userDir, filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将题目写入文件
|
|
|
|
|
* @param questions 题目列表
|
|
|
|
|
* @param outputFile 输出文件
|
|
|
|
|
*/
|
|
|
|
|
private static void writeQuestionsToFile(List<String> questions, File outputFile) {
|
|
|
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter(outputFile))) {
|
|
|
|
|
for (int i = 0; i < questions.size(); i++) {
|
|
|
|
|
writer.println((i + 1) + ". " + questions.get(i));
|
|
|
|
|
@ -344,7 +390,15 @@ public class MathQuestionGenerator {
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.out.println("保存文件失败:" + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 显示生成结果
|
|
|
|
|
* @param count 题目数量
|
|
|
|
|
* @param grade 年级类型
|
|
|
|
|
* @param outputFile 输出文件
|
|
|
|
|
*/
|
|
|
|
|
private static void showGenerationResult(int count, String grade, File outputFile) {
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.println("✓ 题目生成完成!");
|
|
|
|
|
System.out.println("✓ 已生成 " + count + " 道" + grade + "数学题目");
|
|
|
|
|
|