四则运算
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("请输入要做的题目总数:");
int totalQuestions = scanner.nextInt();
// 用户输入各种运算类型的题目数量
System.out.println("请输入加法题数量:");
int addQuestionCount = scanner.nextInt();
System.out.println("请输入减法题数量:");
int subtractQuestionCount = scanner.nextInt();
System.out.println("请输入乘法题数量:");
int multiplyQuestionCount = scanner.nextInt();
System.out.println("请输入除法题数量:");
int divideQuestionCount = scanner.nextInt();
int sumOfUserSpecified = addQuestionCount + subtractQuestionCount + multiplyQuestionCount + divideQuestionCount;
if (sumOfUserSpecified < totalQuestions) {
int remaining = totalQuestions - sumOfUserSpecified;
while (remaining > 0) {
int randomOp = random.nextInt(4);
switch (randomOp) {
case 0:
addQuestionCount++;
break;
case 1:
subtractQuestionCount++;
break;
case 2:
multiplyQuestionCount++;
break;
case 3:
divideQuestionCount++;
break;
}
remaining--;
}
} else if (sumOfUserSpecified > totalQuestions) {
List<Integer> opCounts = new ArrayList<>();
opCounts.add(addQuestionCount);
opCounts.add(subtractQuestionCount);
opCounts.add(multiplyQuestionCount);
opCounts.add(divideQuestionCount);
while (sumOfUserSpecified > totalQuestions) {
int randomIndex = random.nextInt(4);
if (opCounts.get(randomIndex) > 0) {
opCounts.set(randomIndex, opCounts.get(randomIndex) - 1);
sumOfUserSpecified--;
}
}
addQuestionCount = opCounts.get(0);
subtractQuestionCount = opCounts.get(1);
multiplyQuestionCount = opCounts.get(2);
divideQuestionCount = opCounts.get(3);
}
// 设置倒计时两分钟(120000 毫秒)
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("\n时间到!");
System.exit(0);
}
}, 120000);
int correctCount = 0;
int wrongCount = 0;
int questionNumber = 0;
List<Question> wrongQuestions = new ArrayList<>();
while (questionNumber < totalQuestions) {
BigDecimal n1 = null;
BigDecimal n2 = null;
BigDecimal expectedResult = null;
String op = "";
if (addQuestionCount > 0) {
if (random.nextBoolean()) {
n1 = BigDecimal.valueOf(random.nextInt(1000));
n2 = BigDecimal.valueOf(random.nextInt(1000));
} else {
n1 = BigDecimal.valueOf(random.nextDouble() * 1000);
n2 = BigDecimal.valueOf(random.nextDouble() * 1000);
}
op = "+";
expectedResult = BigNumberCalculator.add(n1, n2);
addQuestionCount--;
} else if (subtractQuestionCount > 0) {
if (random.nextBoolean()) {
n1 = BigDecimal.valueOf(random.nextInt(1000));
n2 = BigDecimal.valueOf(random.nextInt(1000));
if (n1.compareTo(n2) < 0) {
BigDecimal temp = n1;
n1 = n2;
n2 = temp;
}
} else {
n1 = BigDecimal.valueOf(random.nextDouble() * 1000);
n2 = BigDecimal.valueOf(random.nextDouble() * 1000);
if (n1.compareTo(n2) < 0) {
BigDecimal temp = n1;
n1 = n2;
n2 = temp;
}
}
op = "-";
expectedResult = BigNumberCalculator.subtract(n1, n2);
subtractQuestionCount--;
} else if (multiplyQuestionCount > 0) {
if (random.nextBoolean()) {
n1 = BigDecimal.valueOf(random.nextInt(100));
n2 = BigDecimal.valueOf(random.nextInt(100));
} else {
n1 = BigDecimal.valueOf(random.nextDouble() * 100);
n2 = BigDecimal.valueOf(random.nextDouble() * 100);
}
op = "*";
expectedResult = BigNumberCalculator.multiply(n1, n2);
multiplyQuestionCount--;
} else if (divideQuestionCount > 0) {
if (random.nextBoolean()) {
n1 = BigDecimal.valueOf(random.nextInt(1000));
n2 = BigDecimal.valueOf(random.nextInt(100));
if (n2.compareTo(BigDecimal.ZERO) == 0) {
n2 = BigDecimal.ONE;
}
} else {
n1 = BigDecimal.valueOf(random.nextDouble() * 1000);
n2 = BigDecimal.valueOf(random.nextDouble() * 100);
if (n2.compareTo(BigDecimal.ZERO) == 0) {
n2 = BigDecimal.ONE;
}
}
op = "/";
expectedResult = BigNumberCalculator.divide(n1, n2);
divideQuestionCount--;
}
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(n1) + " " + op + " " + df.format(n2) + " =");
while (!scanner.hasNextBigDecimal()) {
System.out.println("请输入浮点数或大数答案:");
scanner.next();
}
BigDecimal userAnswer = scanner.nextBigDecimal();
if (userAnswer.compareTo(expectedResult) == 0) {
System.out.println("正确!");
correctCount++;
questionNumber++;
} else {
System.out.println("错误!正确答案是:" + df.format(expectedResult));
wrongCount++;
questionNumber++;
wrongQuestions.add(new Question(df.format(n1), op, df.format(n2), expectedResult, userAnswer));
}
}
timer.cancel();
System.out.println("错题数:" + wrongCount);
double accuracy = (correctCount / (double) totalQuestions) * 100;
// 使用 DecimalFormat 格式化输出结果,保留两位小数
DecimalFormat dfAccuracy = new DecimalFormat("0.00");
System.out.println("正确率:" + dfAccuracy.format(accuracy) + "%");
boolean continueTesting = true;
while (continueTesting) {
System.out.println("\n是否进行下一套题目答题?(y/n)");
String nextChoice = scanner.next();
if (nextChoice.equalsIgnoreCase("y")) {
// 重置计数器和题目数量
correctCount = 0;
wrongCount = 0;
questionNumber = 0;
wrongQuestions.clear();
System.out.println("请输入要做的题目总数:");
totalQuestions = scanner.nextInt();
System.out.println("请输入加法题数量:");
addQuestionCount = scanner.nextInt();
System.out.println("请输入减法题数量:");
subtractQuestionCount = scanner.nextInt();
System.out.println("请输入乘法题数量:");
multiplyQuestionCount = scanner.nextInt();
System.out.println("请输入除法题数量:");
divideQuestionCount = scanner.nextInt();
sumOfUserSpecified = addQuestionCount + subtractQuestionCount + multiplyQuestionCount + divideQuestionCount;
if (sumOfUserSpecified < totalQuestions) {
int remaining = totalQuestions - sumOfUserSpecified;
while (remaining > 0) {
int randomOp = random.nextInt(4);
switch (randomOp) {
case 0:
addQuestionCount++;
break;
case 1:
subtractQuestionCount++;
break;
case 2:
multiplyQuestionCount++;
break;
case 3:
divideQuestionCount++;
break;
}
remaining--;
}
} else if (sumOfUserSpecified > totalQuestions) {
List<Integer> opCounts = new ArrayList<>();
opCounts.add(addQuestionCount);
opCounts.add(subtractQuestionCount);
opCounts.add(multiplyQuestionCount);
opCounts.add(divideQuestionCount);
while (sumOfUserSpecified > totalQuestions) {
int randomIndex = random.nextInt(4);
if (opCounts.get(randomIndex) > 0) {
opCounts.set(randomIndex, opCounts.get(randomIndex) - 1);
sumOfUserSpecified--;
}
}
addQuestionCount = opCounts.get(0);
subtractQuestionCount = opCounts.get(1);
multiplyQuestionCount = opCounts.get(2);
divideQuestionCount = opCounts.get(3);
}
// 重新开始答题循环
while (questionNumber < totalQuestions) {
BigDecimal n1New = null;
BigDecimal n2New = null;
BigDecimal expectedResultNew = null;
String opNew = "";
if (addQuestionCount > 0) {
if (random.nextBoolean()) {
n1New = BigDecimal.valueOf(random.nextInt(1000));
n2New = BigDecimal.valueOf(random.nextInt(1000));
} else {
n1New = BigDecimal.valueOf(random.nextDouble() * 1000);
n2New = BigDecimal.valueOf(random.nextDouble() * 1000);
}
opNew = "+";
expectedResultNew = BigNumberCalculator.add(n1New, n2New);
addQuestionCount--;
} else if (subtractQuestionCount > 0) {
if (random.nextBoolean()) {
n1New = BigDecimal.valueOf(random.nextInt(1000));
n2New = BigDecimal.valueOf(random.nextInt(1000));
if (n1New.compareTo(n2New) < 0) {
BigDecimal temp = n1New;
n1New = n2New;
n2New = temp;
}
} else {
n1New = BigDecimal.valueOf(random.nextDouble() * 1000);
n2New = BigDecimal.valueOf(random.nextDouble() * 1000);
if (n1New.compareTo(n2New) < 0) {
BigDecimal temp = n1New;
n1New = n2New;
n2New = temp;
}
}
opNew = "-";
expectedResultNew = BigNumberCalculator.subtract(n1New, n2New);
subtractQuestionCount--;
} else if (multiplyQuestionCount > 0) {
if (random.nextBoolean()) {
n1New = BigDecimal.valueOf(random.nextInt(100));
n2New = BigDecimal.valueOf(random.nextInt(100));
} else {
n1New = BigDecimal.valueOf(random.nextDouble() * 100);
n2New = BigDecimal.valueOf(random.nextDouble() * 100);
}
opNew = "*";
expectedResultNew = BigNumberCalculator.multiply(n1New, n2New);
multiplyQuestionCount--;
} else if (divideQuestionCount > 0) {
if (random.nextBoolean()) {
n1New = BigDecimal.valueOf(random.nextInt(1000));
n2New = BigDecimal.valueOf(random.nextInt(100));
if (n2New.compareTo(BigDecimal.ZERO) == 0) {
n2New = BigDecimal.ONE;
}
} else {
n1New = BigDecimal.valueOf(random.nextDouble() * 1000);
n2New = BigDecimal.valueOf(random.nextDouble() * 100);
if (n2New.compareTo(BigDecimal.ZERO) == 0) {
n2New = BigDecimal.ONE;
}
}
opNew = "/";
expectedResultNew = BigNumberCalculator.divide(n1New, n2New);
divideQuestionCount--;
}
DecimalFormat dfNew = new DecimalFormat("0.00");
System.out.println(dfNew.format(n1New) + " " + opNew + " " + dfNew.format(n2New) + " =");
while (!scanner.hasNextBigDecimal()) {
System.out.println("请输入浮点数或大数答案:");
scanner.next();
}
BigDecimal userAnswerNew = scanner.nextBigDecimal();
if (userAnswerNew.compareTo(expectedResultNew) == 0) {
System.out.println("正确!");
correctCount++;
questionNumber++;
} else {
System.out.println("错误!正确答案是:" + dfNew.format(expectedResultNew));
wrongCount++;
questionNumber++;
wrongQuestions.add(new Question(dfNew.format(n1New), opNew, dfNew.format(n2New), expectedResultNew, userAnswerNew));
}
}
timer.cancel();
System.out.println("错题数:" + wrongCount);
accuracy = (correctCount / (double) totalQuestions) * 100;
// 使用 DecimalFormat 格式化输出结果,保留两位小数
dfAccuracy = new DecimalFormat("0.00");
System.out.println("正确率:" + dfAccuracy.format(accuracy) + "%");
} else {
continueTesting = false;
}
}
// 错题集功能
if (!wrongQuestions.isEmpty()) {
System.out.println("\n错题集:");
for (Question question : wrongQuestions) {
System.out.println(question.n1 + " " + question.op + " " + question.n2 + " = (你的答案:" + question.userAnswer + ", 正确答案:" + question.expectedAnswer + ")");
}
}
// 错题重练功能
System.out.println("\n是否进行错题重练?(y/n)");
String choice = scanner.next();
if (choice.equalsIgnoreCase("y")) {
int retryCorrectCount = 0;
int retryWrongCount = 0;
for (Question question : wrongQuestions) {
System.out.println(question.n1 + " " + question.op + " " + question.n2 + " =");
while (!scanner.hasNextBigDecimal()) {
System.out.println("请输入浮点数或大数答案:");
scanner.next();
}
BigDecimal retryUserAnswer = scanner.nextBigDecimal();
if (retryUserAnswer.compareTo(question.expectedAnswer) == 0) {
System.out.println("正确!");
retryCorrectCount++;
} else {
System.out.println("错误!正确答案是:" + question.expectedAnswer);
retryWrongCount++;
}
}
System.out.println("错题重练结果:正确 " + retryCorrectCount + " 题,错误 " + retryWrongCount + " 题。");
}
}
}
class BigNumberCalculator {
public static BigDecimal add(BigDecimal num1, BigDecimal num2) {
return num1.add(num2);
}
public static BigDecimal subtract(BigDecimal num1, BigDecimal num2) {
return num1.subtract(num2);
}
public static BigDecimal multiply(BigDecimal num1, BigDecimal num2) {
return num1.multiply(num2);
}
public static BigDecimal divide(BigDecimal num1, BigDecimal num2) {
if (num2.compareTo(BigDecimal.ZERO) == 0) {
throw new ArithmeticException("除数不能为零");
}
return num1.divide(num2, 2, BigDecimal.ROUND_HALF_UP);
}
}
class Question {
String n1;
String op;
String n2;
BigDecimal expectedAnswer;
BigDecimal userAnswer;
public Question(String n1, String op, String n2, BigDecimal expectedAnswer, BigDecimal userAnswer) {
this.n1 = n1;
this.op = op;
this.n2 = n2;
this.expectedAnswer = expectedAnswer;
this.userAnswer = userAnswer;
}
}