pull/4/head
黄培毅 4 days ago
parent dd141bb546
commit 770eff5664

@ -5,32 +5,32 @@ import java.util.Random;
*
*/
public class JuniorQuestionGenerator implements QuestionGenerator {
@Override
public String generateQuestion(StringBuilder question, Random random) {
// 确保所有初中题目都包含至少一个平方或开根号的运算符
int num = random.nextInt(100) + 1;
boolean isSquare = random.nextBoolean();
@Override
public String generateQuestion(StringBuilder question, Random random) {
// 确保所有初中题目都包含至少一个平方或开根号的运算符
int num = random.nextInt(100) + 1;
boolean isSquare = random.nextBoolean();
if (isSquare) {
// 生成平方题
question.append(num).append("^2");
} else {
// 生成开根号题,确保是完全平方数
int sqrtNum = random.nextInt(10) + 1;
num = sqrtNum * sqrtNum;
question.append("√").append(num);
}
// 添加一些其他运算符使题目更复杂
int complexity = random.nextInt(2) + 1;
for (int i = 0; i < complexity; i++) {
String[] operators = {"+", "-", "*"};
String op = operators[random.nextInt(operators.length)];
int addNum = random.nextInt(10) + 1;
question.append(" ").append(op).append(" ").append(addNum);
}
if (isSquare) {
// 生成平方题
question.append(num).append("^2");
} else {
// 生成开根号题,确保是完全平方数
int sqrtNum = random.nextInt(10) + 1;
num = sqrtNum * sqrtNum;
question.append("√").append(num);
}
question.append(" = ?");
return question.toString();
// 添加一些其他运算符使题目更复杂
int complexity = random.nextInt(2) + 1;
for (int i = 0; i < complexity; i++) {
String[] operators = {"+", "-", "*"};
String op = operators[random.nextInt(operators.length)];
int addNum = random.nextInt(10) + 1;
question.append(" ").append(op).append(" ").append(addNum);
}
question.append(" = ?");
return question.toString();
}
}

Binary file not shown.

@ -9,300 +9,300 @@ import java.util.*;
*
*/
public class MathExamGenerator {
// 预设的用户账号信息
private static final Map<String, UserInfo> USER_ACCOUNTS = new HashMap<>();
// 当前登录用户信息
private static UserInfo currentUser = null;
// 已生成的题目集,用于检查重复
private static final Set<String> generatedQuestions = new HashSet<>();
private static boolean isRunning = true;
static {
// 初始化小学、初中和高中各三个账号
// 小学账户
USER_ACCOUNTS.put("张三1", new UserInfo("张三1", "123", "小学"));
USER_ACCOUNTS.put("张三2", new UserInfo("张三2", "123", "小学"));
USER_ACCOUNTS.put("张三3", new UserInfo("张三3", "123", "小学"));
// 初中账户
USER_ACCOUNTS.put("李四1", new UserInfo("李四1", "123", "初中"));
USER_ACCOUNTS.put("李四2", new UserInfo("李四2", "123", "初中"));
USER_ACCOUNTS.put("李四3", new UserInfo("李四3", "123", "初中"));
// 高中账户
USER_ACCOUNTS.put("王五1", new UserInfo("王五1", "123", "高中"));
USER_ACCOUNTS.put("王五2", new UserInfo("王五2", "123", "高中"));
USER_ACCOUNTS.put("王五3", new UserInfo("王五3", "123", "高中"));
}
public static void main(String[] args) {
// 使用BufferedReader配合InputStreamReader明确指定GBK编码
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, Charset.forName("GBK")))) {
System.out.println("欢迎使用中小学数学卷子自动生成程序");
System.out.println("请使用您的账号和密码登录(格式:用户名 密码)");
while (isRunning) {
if (currentUser == null) {
handleLogIn(reader);
} else {
handleRequirement(reader);
}
}
} catch (IOException e) {
System.out.println("输入输出错误:" + e.getMessage());
}
}
public static void handleLogIn(BufferedReader reader) throws IOException {
// 用户未登录,显示登录提示
System.out.print("请输入用户名和密码(用空格隔开,输入exit退出");
String input = reader.readLine();
if (input == null || input.trim().isEmpty()) {
System.out.println("输入不能为空,请重新输入");
return;
} else if (input.equals("exit")) {
isRunning = false;
return;
}
String[] parts = input.split(" ");
if (parts.length == 2) {
String username = parts[0];
String password = parts[1];
// 验证用户登录
boolean loginSuccess = false;
for (Map.Entry<String, UserInfo> entry : USER_ACCOUNTS.entrySet()) {
if (entry.getValue().getUsername().equals(username) &&
entry.getValue().getPassword().equals(password)) {
currentUser = entry.getValue();
loginSuccess = true;
break;
}
}
if (loginSuccess) {
System.out.println("登录成功!当前选择为" + currentUser.getUserType() + "出题");
System.out.println("提示:您可以输入'切换为小学'、'切换为初中'或'切换为高中'来切换题目难度");
handleUserChoice();
loadExistingQuestions();
} else {
System.out.println("登录失败,请输入正确的用户名和密码");
}
// 预设的用户账号信息
private static final Map<String, UserInfo> USER_ACCOUNTS = new HashMap<>();
// 当前登录用户信息
private static UserInfo currentUser = null;
// 已生成的题目集,用于检查重复
private static final Set<String> generatedQuestions = new HashSet<>();
private static boolean isRunning = true;
static {
// 初始化小学、初中和高中各三个账号
// 小学账户
USER_ACCOUNTS.put("张三1", new UserInfo("张三1", "123", "小学"));
USER_ACCOUNTS.put("张三2", new UserInfo("张三2", "123", "小学"));
USER_ACCOUNTS.put("张三3", new UserInfo("张三3", "123", "小学"));
// 初中账户
USER_ACCOUNTS.put("李四1", new UserInfo("李四1", "123", "初中"));
USER_ACCOUNTS.put("李四2", new UserInfo("李四2", "123", "初中"));
USER_ACCOUNTS.put("李四3", new UserInfo("李四3", "123", "初中"));
// 高中账户
USER_ACCOUNTS.put("王五1", new UserInfo("王五1", "123", "高中"));
USER_ACCOUNTS.put("王五2", new UserInfo("王五2", "123", "高中"));
USER_ACCOUNTS.put("王五3", new UserInfo("王五3", "123", "高中"));
}
public static void main(String[] args) {
// 使用BufferedReader配合InputStreamReader明确指定GBK编码
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, Charset.forName("GBK")))) {
System.out.println("欢迎使用中小学数学卷子自动生成程序");
System.out.println("请使用您的账号和密码登录(格式:用户名 密码)");
while (isRunning) {
if (currentUser == null) {
handleLogIn(reader);
} else {
System.out.println("输入格式错误,请用空格分隔用户名和密码");
handleRequirement(reader);
}
}
} catch (IOException e) {
System.out.println("输入输出错误:" + e.getMessage());
}
private static void handleRequirement(BufferedReader reader) throws IOException {
// 用户已登录,显示题目数量输入提示
System.out.print("准备生成" + currentUser.getUserType() +
"数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录");
String input = reader.readLine();
if (input == null || input.trim().isEmpty()) {
return;
}
// 检查是否为切换类型命令
if (input.startsWith("切换为")) {
String targetType = input.substring(3).trim();
if ("小学".equals(targetType) || "初中".equals(targetType) || "高中".equals(targetType)) {
currentUser.setUserType(targetType);
System.out.println("切换成功!准备生成" + currentUser.getUserType() + "数学题目,请输入生成题目数量");
} else {
System.out.println("切换类型错误,请输入'切换为小学'、'切换为初中'或'切换为高中'");
}
return;
}
try {
int questionCount = Integer.parseInt(input);
if (questionCount == -1) {
// 退出登录
currentUser = null;
generatedQuestions.clear();
System.out.println("已退出登录");
} else if (questionCount >= 10 && questionCount <= 30) {
// 生成题目
generateExam(questionCount);
} else {
System.out.println("题目数量必须在10-30之间");
}
} catch (NumberFormatException e) {
System.out.println("请输入有效的数字");
}
}
public static void handleLogIn(BufferedReader reader) throws IOException {
// 用户未登录,显示登录提示
System.out.print("请输入用户名和密码(用空格隔开,输入exit退出");
String input = reader.readLine();
if (input == null || input.trim().isEmpty()) {
System.out.println("输入不能为空,请重新输入");
return;
} else if (input.equals("exit")) {
isRunning = false;
return;
}
// 生成试卷
private static void generateExam(int questionCount) {
List<String> questions = generateUniqueQuestions(questionCount);
// 保存试卷到文件
saveExamToFile(questions);
System.out.println("已成功生成" + questions.size() + "道" + currentUser.getUserType() + "数学题目");
String[] parts = input.split(" ");
if (parts.length == 2) {
String username = parts[0];
String password = parts[1];
// 验证用户登录
boolean loginSuccess = false;
for (Map.Entry<String, UserInfo> entry : USER_ACCOUNTS.entrySet()) {
if (entry.getValue().getUsername().equals(username) &&
entry.getValue().getPassword().equals(password)) {
currentUser = entry.getValue();
loginSuccess = true;
break;
}
}
if (loginSuccess) {
System.out.println("登录成功!当前选择为" + currentUser.getUserType() + "出题");
System.out.println("提示:您可以输入'切换为小学'、'切换为初中'或'切换为高中'来切换题目难度");
handleUserChoice();
loadExistingQuestions();
} else {
System.out.println("登录失败,请输入正确的用户名和密码");
}
} else {
System.out.println("输入格式错误,请用空格分隔用户名和密码");
}
// 生成不重复的题目
private static List<String> generateUniqueQuestions(int questionCount) {
List<String> questions = new ArrayList<>();
Random random = new Random();
int attempts = 0;
int maxAttempts = questionCount * 3; // 最多尝试次数,避免死循环
while (questions.size() < questionCount && attempts < maxAttempts) {
String question = generateQuestion();
if (!generatedQuestions.contains(question)) {
questions.add(question);
generatedQuestions.add(question);
}
attempts++;
}
return questions;
}
private static void handleRequirement(BufferedReader reader) throws IOException {
// 用户已登录,显示题目数量输入提示
System.out.print("准备生成" + currentUser.getUserType() +
"数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录");
String input = reader.readLine();
if (input == null || input.trim().isEmpty()) {
return;
}
// 检查是否为切换类型命令
if (input.startsWith("切换为")) {
String targetType = input.substring(3).trim();
if ("小学".equals(targetType) || "初中".equals(targetType) || "高中".equals(targetType)) {
currentUser.setUserType(targetType);
System.out.println("切换成功!准备生成" + currentUser.getUserType() + "数学题目,请输入生成题目数量");
} else {
System.out.println("切换类型错误,请输入'切换为小学'、'切换为初中'或'切换为高中'");
}
return;
}
// 处理用户生成试卷后的选择
private static void handleUserChoice() {
System.out.println("请选择:");
System.out.println("1. 生成题目");
System.out.println("2. 切换题目难度");
System.out.println("3. 退出当前用户");
System.out.println("4. 退出程序");
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, Charset.forName("GBK")));
String choice = reader.readLine();
switch (choice) {
case "2":
handleDifficultySwitch();
break;
case "3":
currentUser = null;
generatedQuestions.clear();
System.out.println("已退出登录");
break;
case "1":
break;
case "4":
isRunning = false;
break;
default:
System.out.println("无效的选项,请重新输入");
handleUserChoice();
break;
}
} catch (IOException e) {
System.out.println("输入错误:" + e.getMessage());
}
try {
int questionCount = Integer.parseInt(input);
if (questionCount == -1) {
// 退出登录
currentUser = null;
generatedQuestions.clear();
System.out.println("已退出登录");
} else if (questionCount >= 10 && questionCount <= 30) {
// 生成题目
generateExam(questionCount);
} else {
System.out.println("题目数量必须在10-30之间");
}
} catch (NumberFormatException e) {
System.out.println("请输入有效的数字");
}
}
// 生成试卷
private static void generateExam(int questionCount) {
List<String> questions = generateUniqueQuestions(questionCount);
// 处理难度切换
private static void handleDifficultySwitch() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, Charset.forName("GBK")));
while (true) {
System.out.print("请输入目标难度(小学/初中/高中):");
String targetType = reader.readLine();
if ("小学".equals(targetType) || "初中".equals(targetType) || "高中".equals(targetType)) {
currentUser.setUserType(targetType);
System.out.println("已切换为" + targetType + "难度");
break;
} else {
System.out.println("难度类型错误,请重新输入");
}
}
} catch (IOException e) {
System.out.println("输入错误:" + e.getMessage());
}
// 保存试卷到文件
saveExamToFile(questions);
System.out.println("已成功生成" + questions.size() + "道" + currentUser.getUserType() + "数学题目");
handleUserChoice();
}
// 生成不重复的题目
private static List<String> generateUniqueQuestions(int questionCount) {
List<String> questions = new ArrayList<>();
Random random = new Random();
int attempts = 0;
int maxAttempts = questionCount * 3; // 最多尝试次数,避免死循环
while (questions.size() < questionCount && attempts < maxAttempts) {
String question = generateQuestion();
if (!generatedQuestions.contains(question)) {
questions.add(question);
generatedQuestions.add(question);
}
attempts++;
}
// 根据用户类型获取对应的题目生成器
private static QuestionGenerator getQuestionGenerator() {
if ("小学".equals(currentUser.getUserType())) {
return new PrimaryQuestionGenerator();
} else if ("初中".equals(currentUser.getUserType())) {
return new JuniorQuestionGenerator();
} else if ("高中".equals(currentUser.getUserType())) {
return new SeniorQuestionGenerator();
return questions;
}
// 处理用户生成试卷后的选择
private static void handleUserChoice() {
System.out.println("请选择:");
System.out.println("1. 生成题目");
System.out.println("2. 切换题目难度");
System.out.println("3. 退出当前用户");
System.out.println("4. 退出程序");
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, Charset.forName("GBK")));
String choice = reader.readLine();
switch (choice) {
case "2":
handleDifficultySwitch();
break;
case "3":
currentUser = null;
generatedQuestions.clear();
System.out.println("已退出登录");
break;
case "1":
break;
case "4":
isRunning = false;
break;
default:
System.out.println("无效的选项,请重新输入");
handleUserChoice();
break;
}
} catch (IOException e) {
System.out.println("输入错误:" + e.getMessage());
}
}
// 处理难度切换
private static void handleDifficultySwitch() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, Charset.forName("GBK")));
while (true) {
System.out.print("请输入目标难度(小学/初中/高中):");
String targetType = reader.readLine();
if ("小学".equals(targetType) || "初中".equals(targetType) || "高中".equals(targetType)) {
currentUser.setUserType(targetType);
System.out.println("已切换为" + targetType + "难度");
break;
} else {
System.out.println("难度类型错误,请重新输入");
}
return null;
}
} catch (IOException e) {
System.out.println("输入错误:" + e.getMessage());
}
}
// 根据用户类型获取对应的题目生成器
private static QuestionGenerator getQuestionGenerator() {
if ("小学".equals(currentUser.getUserType())) {
return new PrimaryQuestionGenerator();
} else if ("初中".equals(currentUser.getUserType())) {
return new JuniorQuestionGenerator();
} else if ("高中".equals(currentUser.getUserType())) {
return new SeniorQuestionGenerator();
}
return null;
}
// 根据用户类型生成题目
private static String generateQuestion() {
StringBuilder question = new StringBuilder();
Random random = new Random();
QuestionGenerator generator = getQuestionGenerator();
if (generator != null) {
return generator.generateQuestion(question, random);
}
// 根据用户类型生成题目
private static String generateQuestion() {
StringBuilder question = new StringBuilder();
Random random = new Random();
QuestionGenerator generator = getQuestionGenerator();
return "题目生成错误";
if (generator != null) {
return generator.generateQuestion(question, random);
}
// 加载已存在的题目,避免重复
private static void loadExistingQuestions() {
if (currentUser == null) return;
try {
String userDir = currentUser.getUsername();
File dir = new File(userDir);
if (dir.exists() && dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (!line.isEmpty() && !line.matches("^\\d+\\.")) {
generatedQuestions.add(line);
}
}
}
}
}
return "题目生成错误";
}
// 加载已存在的题目,避免重复
private static void loadExistingQuestions() {
if (currentUser == null) return;
try {
String userDir = currentUser.getUsername();
File dir = new File(userDir);
if (dir.exists() && dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (!line.isEmpty() && !line.matches("^\\d+\\.")) {
generatedQuestions.add(line);
}
}
}
}
} catch (IOException e) {
System.out.println("加载已存在题目时出错:" + e.getMessage());
}
}
}
} catch (IOException e) {
System.out.println("加载已存在题目时出错:" + e.getMessage());
}
// 保存试卷到文件
private static void saveExamToFile(List<String> questions) {
try {
// 创建用户目录(相对路径)
String userDir = currentUser.getUsername();
Files.createDirectories(Paths.get(userDir));
// 生成文件名:年-月-日-时-分-秒.txt
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
String filePath = userDir + File.separator + timestamp + ".txt";
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath), Charset.forName("GBK")))) {
for (int i = 0; i < questions.size(); i++) {
writer.write((i + 1) + ". " + questions.get(i));
writer.newLine();
if (i < questions.size() - 1) {
writer.newLine(); // 题目之间空一行
}
}
}
} catch (IOException e) {
System.out.println("保存试卷时出错:" + e.getMessage());
}
// 保存试卷到文件
private static void saveExamToFile(List<String> questions) {
try {
// 创建用户目录(相对路径)
String userDir = currentUser.getUsername();
Files.createDirectories(Paths.get(userDir));
// 生成文件名:年-月-日-时-分-秒.txt
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
String filePath = userDir + File.separator + timestamp + ".txt";
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath), Charset.forName("GBK")))) {
for (int i = 0; i < questions.size(); i++) {
writer.write((i + 1) + ". " + questions.get(i));
writer.newLine();
if (i < questions.size() - 1) {
writer.newLine(); // 题目之间空一行
}
}
}
} catch (IOException e) {
System.out.println("保存试卷时出错:" + e.getMessage());
}
}
}

@ -5,37 +5,37 @@ import java.util.Random;
*
*/
public class PrimaryQuestionGenerator implements QuestionGenerator {
@Override
public String generateQuestion(StringBuilder question, Random random) {
int num1 = random.nextInt(100) + 1; // 1-100的随机数
int num2 = random.nextInt(100) + 1;
String[] operators = {"+", "-", "*", "/"};
String op = operators[random.nextInt(operators.length)];
@Override
public String generateQuestion(StringBuilder question, Random random) {
int num1 = random.nextInt(100) + 1; // 1-100的随机数
int num2 = random.nextInt(100) + 1;
String[] operators = {"+", "-", "*", "/"};
String op = operators[random.nextInt(operators.length)];
// 确保减法和除法结果为正整数
if ("-".equals(op) && num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
} else if ("/".equals(op)) {
// 确保除法结果为整数
num1 = num2 * (random.nextInt(10) + 1);
}
// 10%的概率添加括号
if (random.nextDouble() < 0.1) {
question.append("(");
question.append(num1);
question.append(" ").append(op).append(" ");
question.append(num2);
question.append(")");
} else {
question.append(num1);
question.append(" ").append(op).append(" ");
question.append(num2);
}
// 确保减法和除法结果为正整数
if ("-".equals(op) && num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
} else if ("/".equals(op)) {
// 确保除法结果为整数
num1 = num2 * (random.nextInt(10) + 1);
}
question.append(" = ?");
return question.toString();
// 10%的概率添加括号
if (random.nextDouble() < 0.1) {
question.append("(");
question.append(num1);
question.append(" ").append(op).append(" ");
question.append(num2);
question.append(")");
} else {
question.append(num1);
question.append(" ").append(op).append(" ");
question.append(num2);
}
question.append(" = ?");
return question.toString();
}
}

@ -5,11 +5,11 @@ import java.util.Random;
*
*/
public interface QuestionGenerator {
/**
*
* @param question StringBuilder
* @param random
* @return
*/
String generateQuestion(StringBuilder question, Random random);
/**
*
* @param question StringBuilder
* @param random
* @return
*/
String generateQuestion(StringBuilder question, Random random);
}

@ -5,25 +5,25 @@ import java.util.Random;
*
*/
public class SeniorQuestionGenerator implements QuestionGenerator {
@Override
public String generateQuestion(StringBuilder question, Random random) {
// 确保所有高中题目都包含至少一个sin、cos或tan的运算符
String[] functions = {"sin(", "cos(", "tan("};
String func = functions[random.nextInt(functions.length)];
int angle = random.nextInt(100)+1;
@Override
public String generateQuestion(StringBuilder question, Random random) {
// 确保所有高中题目都包含至少一个sin、cos或tan的运算符
String[] functions = {"sin(", "cos(", "tan("};
String func = functions[random.nextInt(functions.length)];
int angle = random.nextInt(100)+1;
question.append(func).append(angle).append(")");
question.append(func).append(angle).append(")");
// 添加一些其他运算符使题目更复杂
int complexity = random.nextInt(3);
for (int i = 0; i < complexity; i++) {
String[] operators = {"+", "-", "*", "/"};
String op = operators[random.nextInt(operators.length)];
int num = random.nextInt(10) + 1;
question.append(" ").append(op).append(" " ).append(num);
}
question.append(" = ?");
return question.toString();
// 添加一些其他运算符使题目更复杂
int complexity = random.nextInt(3);
for (int i = 0; i < complexity; i++) {
String[] operators = {"+", "-", "*", "/"};
String op = operators[random.nextInt(operators.length)];
int num = random.nextInt(10) + 1;
question.append(" ").append(op).append(" " ).append(num);
}
question.append(" = ?");
return question.toString();
}
}

@ -3,51 +3,51 @@
*
*/
public class UserInfo {
private String username;
private String password;
private String userType;
private String username;
private String password;
private String userType;
/**
*
* @param username
* @param password
* @param userType //
*/
public UserInfo(String username, String password, String userType) {
this.username = username;
this.password = password;
this.userType = userType;
}
/**
*
* @param username
* @param password
* @param userType //
*/
public UserInfo(String username, String password, String userType) {
this.username = username;
this.password = password;
this.userType = userType;
}
/**
*
* @return
*/
public String getUsername() {
return username;
}
/**
*
* @return
*/
public String getUsername() {
return username;
}
/**
*
* @return
*/
public String getPassword() {
return password;
}
/**
*
* @return
*/
public String getPassword() {
return password;
}
/**
*
* @return
*/
public String getUserType() {
return userType;
}
/**
*
* @return
*/
public String getUserType() {
return userType;
}
/**
*
* @param userType
*/
public void setUserType(String userType) {
this.userType = userType;
}
/**
*
* @param userType
*/
public void setUserType(String userType) {
this.userType = userType;
}
}
Loading…
Cancel
Save