10.13总结
import java.util.*;
import java.util.concurrent.TimeUnit;
public class ArithmeticPractice {
private Set
private List
private List
private List
private int totalQuestions = 30;
private int timeLimit = 5 * 60; // 5分钟,单位:秒
private Scanner scanner = new Scanner(System.in);
private boolean isTimeOut = false;
public static void main(String[] args) {
ArithmeticPractice practice = new ArithmeticPractice();
practice.generateQuestions();
practice.startPractice();
practice.showResults();
}
// 生成30道不重复的题目
private void generateQuestions() {
System.out.println("正在生成" + totalQuestions + "道题目...");
while (questions.size() < totalQuestions) {
int num1 = (int) (Math.random() * 100);
int num2 = (int) (Math.random() * 100);
int operator = (int) (Math.random() * 4); // 0:+,1:-,2:*,3:/
String opSymbol = "";
int result = 0;
boolean valid = true;
switch (operator) {
case 0: // 加法
opSymbol = "+";
result = num1 + num2;
break;
case 1: // 减法,确保结果非负
opSymbol = "-";
if (num1 < num2) {
valid = false;
} else {
result = num1 - num2;
}
break;
case 2: // 乘法,确保结果不超过三位数
opSymbol = "*";
result = num1 * num2;
if (result >= 1000) {
valid = false;
}
break;
case 3: // 除法,确保能整除且除数不为0
opSymbol = "/";
if (num2 == 0 || num1 % num2 != 0) {
valid = false;
} else {
result = num1 / num2;
}
break;
}
// 检查题目是否有效且不重复
String question = num1 + " " + opSymbol + " " + num2 + " = ?";
if (valid && !generatedQuestions.contains(question)) {
generatedQuestions.add(question);
questions.add(question);
answers.add(result);
}
}
System.out.println("题目生成完毕,准备开始答题!");
}
// 开始答题
private void startPractice() {
System.out.println("\n答题开始!您有" + timeLimit/60 + "分钟时间完成" + totalQuestions + "道题。");
System.out.println("请输入答案,然后按回车确认。");
long startTime = System.currentTimeMillis();
Thread timerThread = new Thread(new TimerRunnable(startTime));
timerThread.start();
for (int i = 0; i < totalQuestions; i++) {
// 检查是否超时
if (System.currentTimeMillis() - startTime > timeLimit * 1000L) {
System.out.println("\n时间到!未完成的题目将视为错误。");
isTimeOut = true;
// 剩余题目标记为错误
for (int j = i; j < totalQuestions; j++) {
results.add(false);
}
break;
}
// 显示当前题目并获取答案
System.out.print("\n第" + (i + 1) + "题: " + questions.get(i));
try {
if (scanner.hasNextInt()) {
int userAnswer = scanner.nextInt();
boolean isCorrect = (userAnswer == answers.get(i));
results.add(isCorrect);
// 即时判断并显示结果
if (isCorrect) {
System.out.println("正确!");
} else {
System.out.println("错误!正确答案是:" + answers.get(i));
}
} else {
// 非数字输入视为错误
scanner.next(); // 清除无效输入
results.add(false);
System.out.println("输入无效!正确答案是:" + answers.get(i));
}
} catch (Exception e) {
results.add(false);
System.out.println("输入错误!正确答案是:" + answers.get(i));
}
}
// 中断计时线程
timerThread.interrupt();
}
// 显示答题结果
private void showResults() {
int correctCount = 0;
double accuracy = (double) correctCount / totalQuestions * 100;
System.out.println("\n总题数: " + totalQuestions);
System.out.println("做对: " + correctCount + "题");
System.out.println("做错: " + (totalQuestions - correctCount) + "题");
System.out.printf("正确率: %.2f%%\n", accuracy);
System.out.println("------------------------------");
}
// 倒计时线程
private class TimerRunnable implements Runnable {
private long startTime;
public TimerRunnable(long startTime) {
this.startTime = startTime;
}
@Override
public void run() {
try {
while (true) {
long elapsedTime = (System.currentTimeMillis() - startTime) / 1000;
long remainingTime = timeLimit - elapsedTime;
if (remainingTime <= 0) {
break;
}
// 每分钟提示一次剩余时间
if (remainingTime % 60 == 0) {
System.out.println("\n剩余时间: " + remainingTime / 60 + "分" + remainingTime % 60 + "秒");
}
// 最后10秒每秒提示
if (remainingTime <= 10) {
System.out.println("\n剩余时间: " + remainingTime + "秒");
}
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e) {
// 线程被中断,正常退出
}
}
}
}