You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.4 KiB
43 lines
1.4 KiB
import java.util.Random;
|
|
|
|
public class HighSchoolQuestionStrategy implements QuestionStrategy {
|
|
private final Random random = new Random();
|
|
private final String[] basicOps = {"+", "-", "*", "/"};
|
|
private final String[] trigFuncs = {"sin", "cos", "tan"};
|
|
|
|
@Override
|
|
public String generateQuestion() {
|
|
// 随机操作数个数 2-5
|
|
int operandsCount = random.nextInt(4) + 2;
|
|
StringBuilder sb = new StringBuilder();
|
|
boolean hasTrig = false;
|
|
|
|
for (int i = 0; i < operandsCount; i++) {
|
|
int num = random.nextInt(100) + 1;
|
|
|
|
// 每个操作数有概率加三角函数
|
|
if (random.nextBoolean()) {
|
|
String func = trigFuncs[random.nextInt(trigFuncs.length)];
|
|
sb.append(func).append("(").append(num).append(")");
|
|
hasTrig = true;
|
|
} else {
|
|
sb.append(num);
|
|
}
|
|
|
|
if (i != operandsCount - 1) {
|
|
String op = basicOps[random.nextInt(basicOps.length)];
|
|
sb.append(" ").append(op).append(" ");
|
|
}
|
|
}
|
|
|
|
// 确保至少一个三角函数
|
|
if (!hasTrig) {
|
|
String func = trigFuncs[random.nextInt(trigFuncs.length)];
|
|
int num = random.nextInt(100) + 1;
|
|
return func + "(" + num + ") + " + sb.toString();
|
|
}
|
|
|
|
return sb.toString();
|
|
}
|
|
}
|