优化上次的四则运算代码;
增加要求:1.添加四年级能够进行五位操作数以内运算;
点击查看代码
import java.util.*;
abstract class MathProblem {
protected int maxOperands; // 最大操作数
protected boolean allowMultiplication; // 是否允许乘法
protected boolean allowDivision; // 是否允许除法
protected boolean allowParentheses; // 是否允许括号
protected int maxRange; // 数值范围
protected Random random = new Random();
public MathProblem(int maxOperands, boolean allowMultiplication, boolean allowDivision, boolean allowParentheses, int maxRange) {
this.maxOperands = maxOperands;
this.allowMultiplication = allowMultiplication;
this.allowDivision = allowDivision;
this.allowParentheses = allowParentheses;
this.maxRange = maxRange;
}
public abstract String generateProblem();
public abstract double evaluate(String expression);
public abstract boolean checkAnswer(String userAnswer, double correctAnswer);
}
class SecondGradeProblem extends MathProblem {
public SecondGradeProblem() {
super(2, true, true, false, 100);
}
@Override
public String generateProblem() {
int a = random.nextInt(maxRange + 1);
int b = random.nextInt(maxRange + 1);
String operator = getRandomOperator();
// 确保除法整除
if (operator.equals("/")) {
if (b == 0) b = 1; // 防止除以0
a -= a % b; // 确保整除
}
return a + " " + operator + " " + b;
}
@Override
public double evaluate(String expression) {
String[] tokens = expression.split(" ");
int a = Integer.parseInt(tokens[0]);
String operator = tokens[1];
int b = Integer.parseInt(tokens[2]);
switch (operator) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
return b != 0 ? a / b : 0;
default:
throw new UnsupportedOperationException("不支持的操作符: " + operator);
}
}
@Override
public boolean checkAnswer(String userAnswer, double correctAnswer) {
return String.valueOf((int) correctAnswer).equals(userAnswer);
}
public String getRandomOperator() {
List<String> operators = new ArrayList<>(Arrays.asList("+", "-", "*", "/"));
return operators.get(random.nextInt(operators.size()));
}
}
class ThirdGradeProblem extends SecondGradeProblem {
public ThirdGradeProblem() {
super();
this.maxOperands = 4; // 操作数最多4个
}
@Override
public String generateProblem() {
StringBuilder problem = new StringBuilder();
int operandCount = random.nextInt(maxOperands - 1) + 2; // 生成2到4个操作数
for (int i = 0; i < operandCount; i++) {
int num = random.nextInt(maxRange + 1);
problem.append(num);
if (i < operandCount - 1) {
problem.append(" ").append(getRandomOperator()).append(" ");
}
}
return problem.toString();
}
}
class FourthGradeProblem extends ThirdGradeProblem {
public FourthGradeProblem() {
super();
this.maxOperands = 5; // 操作数最多5个
}
@Override
public String generateProblem() {
StringBuilder problem = new StringBuilder();
int operandCount = random.nextInt(maxOperands - 1) + 2; // 生成2到5个操作数
boolean useParentheses = random.nextBoolean() && operandCount > 2 && allowParentheses;
return problem.toString();
}
}
// 错题管理类
class WrongProblemManager {
private final Map<String, Integer> wrongProblems = new HashMap<>();
public void addWrongProblem(String problem,double answer) {
wrongProblems.put(problem, wrongProblems.getOrDefault(problem, 0) + 1);
double correctanswer[]={0};
correctanswer[0]=answer;
}
public void displayWrongProblems() {
if (wrongProblems.isEmpty()) {
System.out.println("没有错题记录。");
} else {
System.out.println("错题集:");
wrongProblems.forEach((problem, count) -> System.out.println(problem + " - 错误次数:" + count));
}
}
public void redoWrongProblems() {
Scanner scanner = new Scanner(System.in);
MathProblem problemmath = null;
WrongProblemManager wrongProblemManager = new WrongProblemManager();
wrongProblems.forEach((problem, count) -> System.out.println(problem));
System.out.println("请输入答案:");
String anwser = scanner.next();
try {
double correctAnswer = problemmath.evaluate(problemmath.generateProblem());
if (!problemmath.checkAnswer(anwser, correctAnswer)) {
System.out.println("回答错误!");
} else {
System.out.println("回答正确!");
}
} catch (Exception e) {
System.out.println("计算过程中出错: " + e.getMessage());
}
}
}
public class MathQuiz {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
WrongProblemManager wrongProblemManager = new WrongProblemManager();
while (true) {
System.out.println("请选择年级(2、3、4 或 0退出):");
int grade = scanner.nextInt();
if (grade == 0) break;
System.out.print("输入题目数量:");
int numberOfProblems = scanner.nextInt();
MathProblem problem = null;
switch (grade) {
case 2:
problem = new SecondGradeProblem();
break;
case 3:
problem = new ThirdGradeProblem();
break;
case 4:
problem = new FourthGradeProblem();
break;
default:
System.out.println("无效年级,请重新选择!");
continue;
}
for (int i = 0; i < numberOfProblems; i++) {
String question = problem.generateProblem();
System.out.println("问题 " + (i + 1) + ": " + question);
System.out.print("请输入答案: ");
String userAnswer = scanner.next();
try {
double correctAnswer = problem.evaluate(question);
if (!problem.checkAnswer(userAnswer, correctAnswer)) {
System.out.println("回答错误!");
wrongProblemManager.addWrongProblem(question, correctAnswer);
} else {
System.out.println("回答正确!");
}
} catch (Exception e) {
System.out.println("计算过程中出错: " + e.getMessage());
}
}
System.out.print("是否查看错题本?(Y/N): ");
if (scanner.next().equalsIgnoreCase("Y")) {
wrongProblemManager.displayWrongProblems();
}
System.out.println("是否进行错题练习?(Y/N):");
if (scanner.next().equalsIgnoreCase("Y")) {
}
System.out.print("是否开始下一套题目?(Y/N): ");
if (!scanner.next().equalsIgnoreCase("Y")) {
break;
}
}
scanner.close();
}
private static void domathnumber(int numberOfProblems, String problem,){
}
}