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.
partner_project/src/generator/Problem.java

61 lines
1.7 KiB

package generator;
import java.util.List;
import java.util.Collections;
public class Problem {
private final String expression;
private final double result;
private final List<String> options;
private final String correctAnswerOption;
public Problem(String expression, double result, List<String> options,
String correctAnswerOption) {
this.expression = expression;
this.result = result;
this.options = options;
this.correctAnswerOption = correctAnswerOption;
}
public String getQuestionText() {
return "计算: " + expression + " = ?";
}
public String getExpression() {
return expression;
}
public List<String> getOptions() {
return Collections.unmodifiableList(options);
}
public String getCorrectAnswerOption() {
return formatResult(result);
}
private String formatResult(double result) {
if (Double.isNaN(result)) {
return "Error";
}
if (Math.abs(result - Math.round(result)) < 0.0001) {
return String.valueOf((int) Math.round(result));
}
return String.format("%.2f", result);
}
public double getResult() {
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getQuestionText()).append("\n");
char optionChar = 'A';
for (String option : options) {
sb.append(optionChar++).append(". ").append(option).append(" ");
}
sb.append("\n正确答案: ").append(correctAnswerOption);
return sb.toString();
}
}