|
|
import java.io.IOException;
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
/**
|
|
|
* 程序入口类,负责处理登录和题目生成。
|
|
|
*/
|
|
|
public class Main {
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
User currentUser = null;
|
|
|
|
|
|
// 登录流程
|
|
|
while (true) {
|
|
|
while (currentUser == null) {
|
|
|
System.out.print("请输入用户名和密码,用空格分隔:");
|
|
|
String username = scanner.next();
|
|
|
String password = scanner.next();
|
|
|
|
|
|
currentUser = User.login(username, password);
|
|
|
|
|
|
if (currentUser == null) {
|
|
|
System.out.println("请输入正确的用户名、密码");
|
|
|
} else {
|
|
|
clearScreen(); //登录成功清屏
|
|
|
System.out.println("当前选择为 " + currentUser.getRole() + " 出题");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 每次登录后初始化当前出题类型(账号默认类型)
|
|
|
String currentRole = currentUser.getRole();
|
|
|
|
|
|
// 题目生成流程
|
|
|
while (true) {
|
|
|
System.out.println("准备生成 " + currentRole
|
|
|
+ " 数学题目,请输入生成题目数量(输入-1将退出当前用户,重新登录):");
|
|
|
|
|
|
String input = scanner.next();
|
|
|
|
|
|
// 退出登录
|
|
|
if (input.equals("-1")) {
|
|
|
currentUser = null;
|
|
|
clearScreen();
|
|
|
System.out.println("已退出当前用户,重新登录...");
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
// 检测切换命令
|
|
|
if (input.startsWith("切换为")) {
|
|
|
String newRole = input.substring(3).trim();
|
|
|
if (!newRole.equals("小学") && !newRole.equals("初中") && !newRole.equals("高中")) {
|
|
|
System.out.println("请输入小学、初中和高中三个选项中的一个"+"(当前类型为"+currentRole+")");
|
|
|
continue;
|
|
|
}
|
|
|
currentRole = newRole;
|
|
|
clearScreen();
|
|
|
System.out.println("系统提示:准备生成 " + currentRole + " 数学题目,请输入生成题目数量");
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 输入题目数量
|
|
|
int questionCount;
|
|
|
try {
|
|
|
questionCount = Integer.parseInt(input);
|
|
|
} catch (NumberFormatException e) {
|
|
|
System.out.println("请输入有效的数字或使用“切换为小学/初中/高中”命令"+"(当前类型为"+currentRole+")");
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if (questionCount < 10 || questionCount > 30) {
|
|
|
System.out.println("请输入有效的题目数量 (10-30) 或 -1 退出");
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
clearScreen();
|
|
|
|
|
|
// 把 currentRole 传给 QuestionGenerator
|
|
|
QuestionGenerator generator = new QuestionGenerator(currentUser, currentRole);
|
|
|
generator.generateQuestions(questionCount);
|
|
|
System.out.println("题目已生成并保存!\n");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清屏方法
|
|
|
*/
|
|
|
public static void clearScreen() {
|
|
|
try {
|
|
|
if (System.getProperty("os.name").contains("Windows")) {
|
|
|
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
|
|
|
} else {
|
|
|
new ProcessBuilder("clear").inheritIO().start().waitFor();
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
for (int i = 0; i < 50; i++) {
|
|
|
System.out.println();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|