10月21日记录

news/2024/10/21 23:03:11

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

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/74392.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

24.10.19

A 数学题,不会。 随便取一数 \(v\),询问得到 \(t \equiv \log_g v \pmod p\)。 我们希望找到 \(x\) 使得 \(v^x \equiv g \pmod p\),即 \(g^{tx} \equiv g \pmod p \Leftrightarrow tx \equiv 1 \pmod {p-1}\)。那么只要 \(t\) 与 \(p - 1\) 互质即可求得逆元。 有原根相关知…

git安装-Tortoise git 安装汉化教程

git安装,Tortoise git安装汉化教程,代码版本管理工具1.首先下载 去官网下载 如果下载比较慢的,链接自取 https://pan.quark.cn/s/fcb9d0b39c7f 2. 安装git3. 安装git图形化工具Tortoise git4. 汉化

最新激活Navicat 15教程,附Keygen Patch

前言 大家好,我是小徐啊。navicat是一款常用的数据库连接工具,但是它本身是需要收费的,很不方便。那么,有没有免费的方式呢?今天小徐就介绍下如何激活navicat的方式,永久激活。文末附获取方式。 如何安装 首先,双击navicat的安装包,开始安装,旁边的就是激活工具,待会…

字符串json数组怎么转换成jsonobject类型

字符串数据[]怎么转换成jsonobject格式 String jsonString = "[{\"code\": \"200\", \"msg\": \"读取成功,返回数据条数\", \"data\": [{\"user_id\": \"1\", \"user_name\": \"小…

Python pickle

Python picklepickle在python中 实现对象结构的 序列化和反序列化 python序列化(Pickling)是一个将python对象层次结构转换为 可以本地储存 或者 网络传输的 字节流的过程 python反序列化(unpickling) 是将字节流还原为对象层次结构数据序列化:就是把不能直接储存的数据 储存…

ELK04 ELK综合案例, logstash写入mysql, kibana访问验证 ubuntu使用

6 ELK 综合实战案例6.1 Filebeat 收集Nginx日志利用 Redis 缓存发送至 Elasticsearch 图上ip地址仅供参考 6.1.2.2 修改 Filebeat 配置#安装redis(访问0.0.0.0和密码123456),nginx(访问日志json格式)[root@ubuntu ~]#vim /etc/filebeat/filebeat.yml filebeat.inputs: - type…