首页 > 其他分享 >10月21日记录

10月21日记录

时间:2024-10-21 22:47:38浏览次数:1  
标签:10 21 记录 int mathProblem System totalQuestions new out

下午学习了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();
}

}

标签:10,21,记录,int,mathProblem,System,totalQuestions,new,out
From: https://www.cnblogs.com/pygmy-killer-whale/p/18491568

相关文章

  • 24.10.18
    A如果\(i\)可以继续往前走,那么必然存在\(j\gei>a_j\),对于每个\(i\),将\((a_i,i]\)加一,从\(x\)能走到的最小点就是\(x\)左侧第一个\(0\)。线段树区间加,线段树二分。B要求一条边强制经过,就确定了所有棋子的路径,两条边能同时选当且仅当它们确定的路径一致。用随机......
  • 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,两图的共同点为三条线中有两条满足另外的两条线......
  • 24.10.20
    P3601不互质的数个数就是\(n-\varphi(n)\)。\(\displaystyle\varphi(n)=n\prod\frac{p_i-1}{p_i}\)。直接用小于\(\sqrt{r}\)的素数求欧拉函数。所有数一起求。rep(i,l,r)phi[i-l]=val[i-l]=i;rep(i,1,pcnt) for(LLj=(l+prm[i]-1)/prm[i]......
  • 24.10.19
    A数学题,不会。随便取一数\(v\),询问得到\(t\equiv\log_gv\pmodp\)。我们希望找到\(x\)使得\(v^x\equivg\pmodp\),即\(g^{tx}\equivg\pmodp\Leftrightarrowtx\equiv1\pmod{p-1}\)。那么只要\(t\)与\(p-1\)互质即可求得逆元。有原根相关知识可以知......
  • 2024/10/21 模拟赛总结
    \(100+50+0+5=155\),T3三目没打括号太爽了#A.串串串基本上就是前缀异或和板子交换两个\(0,1\)不会改变奇偶性,所以可以直接疑惑判断//BLuemoon_#include<bits/stdc++.h>usingnamespacestd;constintkMaxN=2e5+5;intn,m,q,l1,r1,l2,r2,f[kMaxN],g[k......
  • 2024-10-21每日一题
    P1223排队接水题目描述有\(n\)个人在一个水龙头前排队接水,假如每个人接水的时间为\(T_i\),请编程找出这\(n\)个人排队的一种顺序,使得\(n\)个人的平均等待时间最小。输入格式第一行为一个整数\(n\)。第二行\(n\)个整数,第\(i\)个整数\(T_i\)表示第\(i\)个人的......
  • 这个大纲旨在为希望深入掌握 .vhdx 文件管理的顶尖用户提供全面的知识体系,涵盖了高级
    VHDX的英语全称是VirtualHardDiskExtendedVHDX(VirtualHardDiskExtended)在Windows虚拟化环境中的优势包括以下几个方面:1. 更大的存储容量最大容量:VHDX文件的最大容量可以达到 64TB,相比之下,传统的VHD(VirtualHardDisk)最大容量仅为 2TB。这使得VHDX更适合需要......
  • 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}\)。不合法的可以归为两种情况:一种是两边都与当前线段相离。另一种是一个与当前线段相交,另一个相离。第一种情......
  • 2024/10/20 模拟赛总结
    \(0+0+0+0=0\),没考#A.袜子分配直接大眼找规律,得到\(n\)双袜子期望为\(\frac{n}{2n-1}\)//BLuemoon_#include<bits/stdc++.h>usingnamespacestd;usingDB=longdouble;DBn;intmain(){freopen("socks.in","r",stdin),freopen("......