这里是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
protected List
protected List
protected List
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