pull/3/head
宋奇峰 5 months ago
parent 313decf7da
commit beddcbcb51

BIN
src/.DS_Store vendored

Binary file not shown.

@ -5,7 +5,7 @@ public class languageSwitch {
case PRIMARY -> "小学";
case MIDDLE -> "初中";
case HIGH -> "高中";
default -> "未知";
//default -> "未知";
};
}
//等级中文输入转level

@ -4,7 +4,7 @@ import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;
public class loadFile {
// 读取该用户文件夹下已有题目的所有题目文本(每行一个题目或跨行拼接)
// 读取该用户文件夹下已有题目的所有题目文本
public static List<String> loadExistingQuestions(String username) {
List<String> all = new ArrayList<>();
Path userDir = Paths.get("data", username);
@ -13,37 +13,37 @@ public class loadFile {
DirectoryStream<Path> ds = Files.newDirectoryStream(userDir, "*.txt");
for (Path p : ds) {
List<String> lines = Files.readAllLines(p, StandardCharsets.UTF_8);
// 将文件中按题号拆分题目 —— 假设格式 "1. xxx" 开头。我们做简单处理:每个题号开头的新题。
// 将文件中按题号拆分题目
StringBuilder cur = new StringBuilder();
for (String line : lines) {
//识别题号
if (line.matches("^\\s*\\d+\\..*")) {
// 新题开始 -> 保存旧题
if (cur.length() > 0) {
// 新题开始 保存旧题
if (!cur.isEmpty()) {
all.add(cur.toString().trim());
}
cur.setLength(0);
//去题号
cur.append(line.replaceFirst("^\\s*\\d+\\.", "").trim());
} else {
// 继续当前题(空行也可能出现)
if (line.trim().isEmpty()) {
// treat as separator; finish current if non-empty
if (cur.length() > 0) {
if (!cur.isEmpty()) {
all.add(cur.toString().trim());
cur.setLength(0);
}
} else {
if (cur.length() > 0) cur.append(" ");
if (!cur.isEmpty()) cur.append(" ");
cur.append(line.trim());
}
}
}
if (cur.length() > 0) all.add(cur.toString().trim());
if (!cur.isEmpty()) all.add(cur.toString().trim());
}
} catch (IOException e) {
// ignore, return what we have
}
// dedupe and return
// 转链式 去空格 去空字符串 去重 转回list
return all.stream().map(String::trim).filter(s->!s.isEmpty()).distinct().collect(Collectors.toList());
}
}
}

@ -1,7 +1,6 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class login {
@ -19,7 +18,6 @@ public class login {
private static final Map<String, Account> accounts = new HashMap<>();
private static final Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
//private static final Random rand = new Random();
// 初始化预设账号
// 初始Map索引

@ -5,10 +5,10 @@ import java.util.stream.Collectors;
public class questionGenerator {
private static final Random rand = new Random();
static class QuestionGenerator {
private final login.Level level;
private final Set<String> existing;
private final int maxAttempts = 2000; // 防止死循环
QuestionGenerator(login.Level level, List<String> existingQuestions) {
this.level = level;
@ -18,6 +18,8 @@ public class questionGenerator {
List<String> generatePaper(int n) {
Set<String> generated = new LinkedHashSet<>();
int attempts = 0;
// 防止死循环
int maxAttempts = 2000;
while (generated.size() < n && attempts < maxAttempts) {
attempts++;
String q = generateOneQuestion();
@ -37,16 +39,14 @@ public class questionGenerator {
return s.replaceAll("\\s+","").toLowerCase();
}
// 生成单题主逻辑
// 生成单题主逻辑
private String generateOneQuestion() {
int operands = rand.nextInt(4) + 2; // 保证 2..5 个操作数
switch (level) {
case PRIMARY: return genPrimary(operands);
case MIDDLE: return genMiddle(operands);
case HIGH: return genHigh(operands);
default: return genPrimary(operands);
}
return switch (level) {
case PRIMARY -> genPrimary(operands);
case MIDDLE -> genMiddle(operands);
case HIGH -> genHigh(operands);
};
}
// 生成小学题(只有 + - * / 和括号)
private String genPrimary(int operands) {

@ -7,6 +7,7 @@ public class savepaper {
// 保存试卷,返回保存路径字符串
public static String savePaper(String username, List<String> paper) {
Path userDir = Paths.get("data", username);
//识别文件夹
try {
if (!Files.exists(userDir)) Files.createDirectories(userDir);
} catch (IOException e) {
@ -18,14 +19,16 @@ public class savepaper {
try (BufferedWriter bw = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
for (int i = 0; i < paper.size(); i++) {
bw.write((i+1) + ". " + paper.get(i));
//换行
bw.newLine();
//空一行
bw.newLine();
}
} catch (IOException e) {
System.out.println("保存文件失败:" + e.getMessage());
return "保存失败";
}
//返回文件位置
return file.toString();
}
}

Loading…
Cancel
Save