pull/2/head
parent
5f5dc962c7
commit
ac7c3053f9
@ -0,0 +1,90 @@
|
||||
package mathlearning.service.MultipleChoiceGenerator;
|
||||
import mathlearning.model.User;
|
||||
import mathlearning.service.QuestionGenerator.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class MultipleChoiceGenerator {
|
||||
private static QuestionGenerator QuestionGenerator = new PrimaryGenerator();
|
||||
String[] QuestionList ;
|
||||
String[] AnswerList ;
|
||||
String[] ChoiceList ;
|
||||
|
||||
MultipleChoiceGenerator(int count, User nowUser){// 如此声明MultipleChoiceGenerator实例,再调用下面三个接口
|
||||
this.QuestionList = new String[count];
|
||||
this.ChoiceList = new String[count];
|
||||
this.QuestionList = SetQuestionList(count, nowUser);
|
||||
SetChoiceList();
|
||||
}
|
||||
|
||||
public String[] GetQuestionList(){
|
||||
return this.QuestionList;
|
||||
}
|
||||
|
||||
public String[] GetChoiceList(){
|
||||
return this.ChoiceList;
|
||||
}
|
||||
|
||||
public String[] GetAnswerList(){
|
||||
return this.AnswerList;
|
||||
}
|
||||
|
||||
private void SetChoiceList(){
|
||||
for(int i = 0 ; i < this.AnswerList.length ; i++){
|
||||
Random random = new Random();
|
||||
String[] choices = new String[4];
|
||||
double correctChoice = Double.parseDouble(this.AnswerList[i]);
|
||||
int position = random.nextInt(4);
|
||||
choices[position] = String.format("%.2f", correctChoice);
|
||||
|
||||
for(int j = 0 ; j < 4 ; j++){
|
||||
if(j != position){
|
||||
double choice = correctChoice;
|
||||
double offset = random.nextInt(41) - 20;
|
||||
choice += offset;
|
||||
choices[j] = String.format("%.2f", choice);
|
||||
}
|
||||
}
|
||||
|
||||
this.ChoiceList[i] = String.join("\n", choices);
|
||||
}
|
||||
}
|
||||
|
||||
private String[] SetQuestionList(int count, User nowUser){
|
||||
String[] Questions= new String[count];
|
||||
if(nowUser.getType().equals("小学") ) {
|
||||
QuestionGenerator = new PrimaryGenerator();
|
||||
}
|
||||
else if( nowUser.getType().equals("初中") ) {
|
||||
QuestionGenerator = new middleGenerator();
|
||||
}
|
||||
else if( nowUser.getType().equals("高中") ) {
|
||||
QuestionGenerator = new highGenerator();
|
||||
}
|
||||
|
||||
List<String> questions = new ArrayList<>();
|
||||
Set<String> generatedQuestions = new HashSet<>();
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
String question;
|
||||
do {
|
||||
question = QuestionGenerator.generateQuestion();
|
||||
} while ( generatedQuestions.contains(question ));
|
||||
generatedQuestions.add(question);
|
||||
questions.add(question);
|
||||
}
|
||||
|
||||
for(int i = 0 ; i< count ; i++){
|
||||
Questions[i] = questions.get(i);
|
||||
}
|
||||
this.AnswerList = new String[count];
|
||||
for(int i = 0 ; i< count ; i++){
|
||||
ComputingUnit computingUnit = new ComputingUnit(Questions[i]);
|
||||
this.AnswerList[i] = computingUnit.getAnswer();
|
||||
}
|
||||
return Questions;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package mathlearning.service.MultipleChoiceGenerator;
|
||||
|
||||
import mathlearning.model.User;
|
||||
|
||||
public class MultipleChoiceGeneratorTest {
|
||||
public static void main(String[] args) {
|
||||
// 创建一个模拟用户
|
||||
User testUser = new User();
|
||||
testUser.setType("小学"); // 可以分别测试"小学"、"初中"、"高中"
|
||||
|
||||
// 测试生成10道题目
|
||||
int questionCount = 10;
|
||||
MultipleChoiceGenerator generator = new MultipleChoiceGenerator(questionCount, testUser);
|
||||
|
||||
// 获取生成的题目、答案和选项
|
||||
String[] questions = generator.GetQuestionList();
|
||||
String[] answers = generator.GetAnswerList();
|
||||
String[] choices = generator.GetChoiceList();
|
||||
|
||||
// 输出测试结果
|
||||
System.out.println("=== 数学题目生成测试 ===");
|
||||
System.out.println("用户类型: " + testUser.getType());
|
||||
System.out.println("生成题目数量: " + questionCount);
|
||||
System.out.println();
|
||||
|
||||
for (int i = 0; i < questions.length; i++) {
|
||||
System.out.println("题目 " + (i + 1) + ": " + questions[i]);
|
||||
System.out.println("答案: " + answers[i]);
|
||||
System.out.println("选项:");
|
||||
String[] choiceArray = choices[i].split("\n");
|
||||
for (int j = 0; j < choiceArray.length; j++) {
|
||||
System.out.println(" " + (char)('A' + j) + ". " + choiceArray[j]);
|
||||
}
|
||||
System.out.println("----------------------------------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package mathlearning.service.QuestionGenerator;
|
||||
|
||||
public class PrimaryGenerator extends QuestionGenerator{
|
||||
public PrimaryGenerator() {
|
||||
super("小学");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
|
||||
int operandCount = random.nextInt(4) + 2;
|
||||
|
||||
// 生成操作数
|
||||
int[] operands = new int[operandCount];
|
||||
for (int i = 0; i < operandCount; i++) {
|
||||
operands[i] = random.nextInt(100) + 1; // 1-100
|
||||
}
|
||||
|
||||
String question = preForOper(operands);
|
||||
return addParen(question);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package mathlearning.service.QuestionGenerator;
|
||||
|
||||
public class highGenerator extends QuestionGenerator{
|
||||
|
||||
public highGenerator() {
|
||||
super("高中");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
int operandCount = random.nextInt(4) + 2;
|
||||
|
||||
// 生成操作数
|
||||
int[] operands = new int[ operandCount ];
|
||||
for (int i = 0; i < operandCount; i++) {
|
||||
operands[i] = random.nextInt(100) + 1; // 1-100
|
||||
}
|
||||
|
||||
String question = preForOper( operands );
|
||||
question = add_squs( question );
|
||||
question = add_sins( question );
|
||||
return addParen( question );
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package mathlearning.service.QuestionGenerator;
|
||||
|
||||
public class middleGenerator extends QuestionGenerator{
|
||||
|
||||
public middleGenerator() {
|
||||
super("初中");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
int operandCount = random.nextInt(4) + 2;
|
||||
|
||||
// 生成操作数
|
||||
int[] operands = new int [ operandCount ];
|
||||
for (int i = 0; i < operandCount; i++) {
|
||||
operands[i] = random.nextInt(100) + 1; // 1-100
|
||||
}
|
||||
|
||||
String question = preForOper(operands);
|
||||
question = add_squs(question);
|
||||
return addParen(question);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue