首页 > 其他分享 >10.21随笔

10.21随笔

时间:2024-10-21 22:59:20浏览次数:8  
标签:10.21 num1 num2 int System totalQuestions 随笔 out

这里是10.21随笔
编程作业留档:要求为自主生成四则运算题,可选择题目数量,难度,并记录错题。
以下为代码:
package ys;

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));
        }
    }
    scanner.close();
}

}

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:
                question = num1 + " - " + num2;
                answer = num1 - num2;
                break;
            case 2:
                question = num1 + " * " + num2;
                answer = num1 * num2;
                break;
            case 3:
                if (num2 == 0) num2 = 1;
                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:
                question = num1 + " - " + num2;
                answer = num1 - num2;
                break;
            case 2:
                question = num1 + " * " + num2;
                answer = num1 * num2;
                break;
            case 3:
                if (num2 == 0) num2 = 1;
                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 MathTest {
public static void main(String[] args) {
Scanner 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("是否查看错题本? (是/否): ");
    if (scanner.next().equalsIgnoreCase("是")) {
        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("是否导出错题本? (是/否): ");
    if (scanner.next().equalsIgnoreCase("是")) {
        System.out.print("请输入文件名: ");
        String filename = scanner.next();
        mathProblem.exportWrongQuestions(filename);
    }

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

    scanner.close();
}

}

标签:10.21,num1,num2,int,System,totalQuestions,随笔,out
From: https://www.cnblogs.com/Thanatos-syst/p/18491571

相关文章

  • 对软件工程的理解(随笔版)
    软件工程是一门复杂且重要的学科。要做好软件开发,首先要有极强的计划性,软件开发并非是一项可以随意进行的工作,它涉及到众多复杂的环节和众多不同专业背景的人员参与。从最初的需求分析到最终的软件上线及后续维护,每一个阶段都需要精心规划和安排。其次,要合理安排时间和进度,合理......
  • 24.10.21
    A哇,直接一个CF*3000。要求的即为图2,5,可以用总方案数(\(\binom{n}{3}\))减去图1,3,4。对于图1,只要求出一根线左边有多少不与它相交的线,右边有多少线,记为\(l_i\)和\(r_i\)。对答案的贡献为\(l_i\timesr_i\)。对于图3,4,两图的共同点为三条线中有两条满足另外的两条线......
  • 10.21日
    CREATETABLEwebsites(idint(11)NOTNULLAUTO_INCREMENT,namechar(20)NOTNULLDEFAULT''COMMENT'站点名称',urlvarchar(255)NOTNULLDEFAULT'',alexaint(11)NOTNULLDEFAULT'0'COMMENT'Alexa排名',co......
  • 10.21
    A.CircleCF297E场上秉持着正难反更难的精神,根本没考虑容斥。正着统计合法方案很难,考虑用总方案数减去不合法方案数。总方案数比较容易求得,为\(\binom{n}{m}\)。不合法的可以归为两种情况:一种是两边都与当前线段相离。另一种是一个与当前线段相交,另一个相离。第一种情......
  • 10.21
    没时间写题了,写点题解。一道题写了一晚上,效率有点低。。。多校A层冲刺NOIP2024模拟赛09区间给定一个长度为\(N\)的数列\(A_1,A_2,\dots,A_N\)和一个长度为\(N−1\)的数列\(B_2,B_3,\dots,B_N\)。有\(Q\)个询问,每次询问是一个区间\([L_i,R_i]\)。请你求出有多少二元......
  • 10.21 ~ 10.27
    10.21Day-4快CSP啦……话说真的应该这么早就开始记“Dayx”吗为啥这几天这么冷啊要冻死了......
  • 24.10.21
    嘛,我是个非常没有动力的人啊现在大概只想躺平哦有时候也可能会有一点点干劲吧,不过属于是过一两个小时就会消失的那种大概是因为没有目标吧,也可以说是没有我特别感兴趣的事其实硬要说感兴趣的事嘛,也有,不过基本都不切实际罢了我倒是想去学钢琴,画画,日语啊啥的,但是家庭条件和生活......
  • 10.21日每日收获
    1、扇区擦除时按首地址擦除,若设定地址不是首地址也从首地址开始擦除,每512个字节为一组,如00H-200H为一组,200H-400H为一组,擦除数据时按组擦除,若果设置擦除开始地址为100h,则仍然会从00H-200H擦除,而不是100H-300H2、有些芯片的FLASHROM结构是类RAM结构,也就是无需擦除可以直接覆盖......
  • 2024.10.21训练记录
    上午NOIP模拟赛A猜了结论。一个一个数做。当前这个数插进去的时候,设前驱为pre[i],后继为nxt[i]。设\(x=max(a[pre[i]],a[nxt[i]]),y=min(a[pre[i]],a[nxt[i]])\)。则:当\(a[i]>x\)时,\(ans+=a[i]-x\);当\(a[i]<y\)时,\(ans+=y-a[i]\);否则\(ans\)不......
  • 24.10.21 FH
    没保存,CaO抢救了一下,详见mysol:A打表。1I2IIVX3IIIIVVIIX4VII5VIII剩余的加X,再加2火柴即可注意没有40!完整:1I2IIVX3IIIIVVIIXXI4VIIXIIXVXX5VIIIXIIIXIVXVIXIXXXI6XVIIXXIIXXVXXX7XVIIIXXIIIXXIVXXVIXXIXXXXI8XXVII......