pull/1/head
Violet 7 months ago
parent 75f10ea2a8
commit 2645345cc4

@ -1,5 +1,11 @@
package src;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.io.IOException;
import java.util.Random;
public class JuniorTea extends Teacher{
public JuniorTea(String name, String password) {
super(name, password);
@ -7,7 +13,62 @@ public class JuniorTea extends Teacher{
@Override
public String GenerateEX(int num, int type){
return null;
// 生成题目存储路径
Path filePath = Paths.get(super.generatePath());
Random random = new Random();
String[] operators = {"+", "-", "*", "/", "^2", "√"};
for(int i=0; i<num; i++){
int operandCount = random.nextInt(4) + 2;
StringBuilder question = new StringBuilder();
int[] operands = new int[operandCount];
for(int j=0; j<operandCount; j++){
operands[j] = random.nextInt(100) + 1;
}
// 随机决定是否以及在哪里加括号 (对于3个以上操作数)
boolean useParentheses = operandCount > 2 && random.nextBoolean();
int parenStartIndex = 0;
if (useParentheses) {
parenStartIndex = random.nextInt(operandCount - 2);
}
for (int j = 0; j < operandCount; j++) {
if (useParentheses && j == parenStartIndex) {
question.append("(");
}
question.append(operands[j]);
if (useParentheses && j == parenStartIndex + 1) {
question.append(")");
}
if(j < operandCount - 1){
String op = operators[random.nextInt(operators.length)];
// 避免除以0
if (op == "/" && operands[j+1] == 0) {
operands[j+1] = random.nextInt(100) + 1;
}
question.append(" " + op + " " );
}
}
String qstr = "(" + (i+1) + ")" + question.toString() + " =\n";
// 题目查重
if(super.checkDuplicate(qstr)){
i--;
continue;
}
// 写入文档
try {
java.nio.file.Files.writeString(filePath, qstr, StandardOpenOption.APPEND);
} catch (IOException e){
e.printStackTrace();
}
}
return filePath.toString();
}
@Override

@ -5,8 +5,10 @@ import java.util.ArrayList;
import java.util.HashMap;
public class Main {
static ArrayList<Teacher> teachers; // 用户列表
static Teacher teacher; // 当前用户
// 用户列表
static ArrayList<Teacher> teachers;
// 当前用户
static Teacher teacher;
static HashMap<String, Integer> typeMap;
public static void main(String[] args) {
final int LEAST_NUM = 10, MAX_NUM = 30;
@ -30,7 +32,8 @@ public class Main {
Scanner scanner = new Scanner(System.in, "GBK");
while(true){
boolean flag = false;
while(!flag){ // 登录界面
// 登录界面
while(!flag){
System.out.println("-------请登录您的账户-------");
System.out.println(" 请输入: 用户名 密码 ");
String cur = scanner.nextLine();
@ -55,7 +58,8 @@ public class Main {
}
}
int type = typeMap.get(teacher.getType()); // 当前出题类型,默认值为当前用户类型
// 当前出题类型,默认值为当前用户类型
int type = typeMap.get(teacher.getType());
boolean logout = false;
while(!logout){
System.out.println("-------是否切换出题类型(y/n)-------");
@ -78,16 +82,17 @@ public class Main {
System.out.println("请继续输入题目数量:");
}
int num = scanner.nextInt();
scanner.nextLine(); // 吸收换行符
scanner.nextLine();
while(true){
try{
if(num == -1){
// 返回到登录界面
System.out.println("已退出登录");
logout = true;
break; // 返回到登录界面
break;
}
if(num >= LEAST_NUM && num <= MAX_NUM){
String filepath = teacher.GenerateEX(num, type); // 生成题目
String filepath = teacher.GenerateEX(num, type);
System.out.println("题目生成成功,已保存至" + filepath);
break;
}

@ -14,8 +14,7 @@ public class PrimaryTea extends Teacher{
@Override
public String GenerateEX(int num, int type){
// 生成题目存储路径
String curPath = super.generatePath();
Path filepath = Paths.get(curPath);
Path filePath = Paths.get(super.generatePath());
Random random = new Random();
char[] operators = {'+', '-', '*', '/'};
@ -47,14 +46,19 @@ public class PrimaryTea extends Teacher{
if (j < operandCount - 1) {
char op = operators[random.nextInt(operators.length)];
// 避免除以0
if (op == '/' && operands[j+1] != 0 && operands[j] % operands[j+1] != 0) {
// 对于小学生,更倾向于可整除的除法
operands[j] = operands[j+1] * (random.nextInt(10) + 1);
// 处理除法--避免除以0 & 保证可整除
if (op == '/') {
if(operands[j+1] == 0) {
operands[j+1] = random.nextInt(100) + 1;
}
if(operands[j] % operands[j+1] != 0) {
operands[j+1] = random.nextInt(10) + 1;
operands[j] = operands[j+1] * (random.nextInt(10) + 1);
}
}
// 避免小学生不熟悉的负数结果
// 避免负数结果
if (op == '-' && operands[j] < operands[j+1]) {
int temp = operands[j];
operands[j] = operands[j+1];
@ -69,18 +73,19 @@ public class PrimaryTea extends Teacher{
}
}
String qstr = "(" + (i + 1) + ") " + question.toString() + " =\n";
if(super.checkDuplicate(qstr)){ // 题目查重
// 题目查重
if(super.checkDuplicate(qstr)){
i--;
continue;
}
// 写入文档
try {
java.nio.file.Files.writeString(filepath, qstr, StandardOpenOption.APPEND);
java.nio.file.Files.writeString(filePath, qstr, StandardOpenOption.APPEND);
} catch (IOException e){
e.printStackTrace();
}
}
return filepath.toString();
return filePath.toString();
}
@Override

@ -2,18 +2,22 @@ package src;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
abstract class Teacher {
String name;
String password;
String path = null;
String path = null; // 教师文件夹路径
public Teacher(String name, String password) {
this.name = name;
this.password = password;
File folder = new File(name); // 生成文件夹
// 生成文件夹
File folder = new File(name);
try{
if(!folder.exists()){
folder.mkdirs();
@ -26,6 +30,22 @@ abstract class Teacher {
// 查重
public boolean checkDuplicate(String question){
File folder = new File(this.path);
File[] files = folder.listFiles();
if(files == null) return false;
for(File file : files){
List<String> lines = null;
try {
lines = Files.readAllLines(Paths.get(file.getAbsolutePath()));
for(String line : lines){
if(line.substring(3).equals(question)){
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}

Loading…
Cancel
Save