10月21日记录

pygmy-killer-whale / 2024-11-07 / 原文

下午学习了java语言继承与派生;
完善了四则运算的二三四年级的代码;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

abstract class MathProblem {
protected List questions;
protected List answers;
protected List wrongQuestions;
protected List wrongAnswers;
protected int totalQuestions;
protected int correctCount;

public MathProblem(int totalQuestions) {
    this.questions = new ArrayList<>();
    this.answers = new ArrayList<>();
    this.wrongQuestions = new ArrayList<>();
    this.wrongAnswers = new ArrayList<>();
    this.totalQuestions = totalQuestions;
    this.correctCount = 0;
}

public abstract void generateProblems();

public void checkAnswers(Map<Integer, Integer> userAnswers) {
    for (Map.Entry<Integer, Integer> entry : userAnswers.entrySet()) {
        int index = entry.getKey();
        int userAnswer = entry.getValue();
        if (userAnswer!= answers.get(index)) {
            wrongQuestions.add(questions.get(index));
            wrongAnswers.add(answers.get(index));
        } else {
            correctCount++;
        }
    }
}

public void showStatistics() {
    System.out.println("总题数: " + totalQuestions);
    System.out.println("正确题数: " + correctCount);
    System.out.println("错误题数: " + (totalQuestions - correctCount));
    System.out.println("正确率: " + (double) correctCount / totalQuestions * 100 + "%");
}

public void exportWrongQuestions(String filename) {
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
        for (int i = 0; i < wrongQuestions.size(); i++) {
            writer.write(wrongQuestions.get(i) + " = " + wrongAnswers.get(i));
            writer.newLine();
        }
        System.out.println("错题已导出到 " + filename);
    } catch (IOException e) {
        System.out.println("导出错题失败: " + e.getMessage());
    }
}

public void recalculateWrongProblems() {

    Scanner scanner = new Scanner(System.in);
    System.out.println("开始重新计算错题:");
    for (int i = 0; i < wrongQuestions.size(); i++) {
        System.out.print(wrongQuestions.get(i) + " = ");
        int userAnswer = scanner.nextInt();
        if (userAnswer == wrongAnswers.get(i)) {
            System.out.println("回答正确!");
            correctCount++;
            wrongQuestions.remove(i);
            wrongAnswers.remove(i);
            i--;
        } else {
            System.out.println("回答错误,正确答案是:" + wrongAnswers.get(i));
        }
    }

}

}

class GradeTwoMath extends MathProblem {
public GradeTwoMath(int totalQuestions) {
super(totalQuestions);
}

@Override
public void generateProblems() {
    Random random = new Random();
    for (int i = 0; i < totalQuestions; i++) {
        int num1 = random.nextInt(101);
        int num2 = random.nextInt(101);
        int operation = random.nextInt(4);
        String question;
        int answer;
        switch (operation) {
            case 0:
                question = num1 + " + " + num2;
                answer = num1 + num2;
                break;
            case 1:
                if(num1-num2<0) num1=num2+1;
                question = num1 + " - " + num2;
                answer = num1 - num2;
                break;
            case 2:
                question = num1 + " * " + num2;
                answer = num1 * num2;
                break;
            case 3:
                if (num2 == 0) num2 = 1;
                if(num1%num2!=0) num1=num2;
                answer = num1 / num2;
                question = num1 + " / " + num2;
                num1 = answer * num2;
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + operation);
        }

        questions.add(question);
        answers.add(answer);
    }
}

}

class GradeThreeMath extends GradeTwoMath {
public GradeThreeMath(int totalQuestions) {
super(totalQuestions);
}

@Override
public void generateProblems() {
    Random random = new Random();
    for (int i = 0; i < totalQuestions; i++) {
        int num1 = random.nextInt(1001);
        int num2 = random.nextInt(1001);
        int operation = random.nextInt(4);
        String question;
        int answer;

        switch (operation) {
            case 0:
                question = num1 + " + " + num2;
                answer = num1 + num2;
                break;
            case 1:
                if(num1-num2<0) num1=num2+1;
                question = num1 + " - " + num2;
                answer = num1 - num2;
                break;
            case 2:
                question = num1 + " * " + num2;
                answer = num1 * num2;
                break;
            case 3:
                if (num2 == 0) num2 = 1;
                if(num1%num2!=0)num1=num2;
                answer = num1 / num2;
                question = num1 + " / " + num2;
                num1 = answer * num2;
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + operation);
        }

        questions.add(question);
        answers.add(answer);
    }
}

}

class GradeFourMath extends GradeThreeMath {
public GradeFourMath(int totalQuestions) {
super(totalQuestions);
}

@Override
public void generateProblems() {
    Random random = new Random();
    for (int i = 0; i < totalQuestions; i++) {
        int num1 = random.nextInt(1001);
        int num2 = random.nextInt(1001);
        int num3 = random.nextInt(1001);
        int operation = random.nextInt(4);
        String question;
        int answer;

        if (random.nextBoolean()) {
            question = "(" + num1 + " + " + num2 + ") * " + num3;
            answer = (num1 + num2) * num3;
        } else {
            question = num1 + " + " + num2 + " * " + num3;
            answer = num1 + num2 * num3;
        }

        questions.add(question);
        answers.add(answer);
    }
}

}

public class Mathquestion {
public static void main(String[] args) {
String choise;
Scanner scanner;
do {
choise = "n";
scanner = new Scanner(System.in);

        System.out.print("请输入题目总数: ");
        int totalQuestions = scanner.nextInt();

        MathProblem mathProblem;
        System.out.print("请选择年级 (2/3/4): ");
        int grade = scanner.nextInt();

        switch (grade) {
            case 2:
                mathProblem = new GradeTwoMath(totalQuestions);
                break;
            case 3:
                mathProblem = new GradeThreeMath(totalQuestions);
                break;
            case 4:
                mathProblem = new GradeFourMath(totalQuestions);
                break;
            default:
                System.out.println("无效的年级选择!");
                return;
        }
        mathProblem.generateProblems();
        System.out.println("请回答以下问题:");
        Map<Integer, Integer> userAnswers = new HashMap<>();
        for (int i = 0; i < totalQuestions; i++) {
            System.out.print((i + 1) + ": " + mathProblem.questions.get(i) + " = ");
            int userAnswer = scanner.nextInt();
            userAnswers.put(i, userAnswer);
        }

        mathProblem.checkAnswers(userAnswers);
        mathProblem.showStatistics();

        System.out.print("是否查看错题本? (y/n): ");
        if (scanner.next().equalsIgnoreCase("y")) {
            System.out.println("错题本:");
            for (int i = 0; i < mathProblem.wrongQuestions.size(); i++) {
                System.out.println(mathProblem.wrongQuestions.get(i) + " = " + mathProblem.wrongAnswers.get(i));
            }
        }

        System.out.print("是否导出错题本? (y/n): ");
        if (scanner.next().equalsIgnoreCase("y")) {
            System.out.print("请输入文件名: ");
            String filename = scanner.next();
            mathProblem.exportWrongQuestions(filename);
        }

        System.out.print("是否重新计算错题? (y/n): ");
        if (scanner.next().equalsIgnoreCase("y")) {
            mathProblem.recalculateWrongProblems();
            mathProblem.showStatistics();
        }

        System.out.print("是否开始下一轮答题?? (y/n): ");
        String jirafeaturetemp = scanner.next();
        if (jirafeaturetemp .equalsIgnoreCase("y")) {
            choise = "y";
        }

    } while (choise.equals("y"));

    scanner.close();
}

}