当前位置: 首页 > news >正文

10.14总结

import java.util.*;
import java.util.concurrent.TimeUnit;

public class ArithmeticPractice {
private Set generatedQuestions = new HashSet<>();
private List questions = new ArrayList<>();
private List answers = new ArrayList<>();
private List results = new ArrayList<>();
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: // 除法,确保能整除且除数不为0opSymbol = "/";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;}@Overridepublic 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) {// 线程被中断,正常退出}}
}

}

http://www.rkmt.cn/news/20560.html

相关文章:

  • 图形学中的变换
  • 使用DirectX绘制天空盒并实现破坏和放置方块
  • DirectX12初始化
  • 10月13日
  • Ubuntu22.04安装CH340/CH341驱动
  • STM32——UART
  • WebApi 交叉观察者- IntersectionObserver复盘
  • css: Bootstrap5 Accordions
  • AMPopTip - 优雅的iOS动画提示框库
  • 文件名中有空格比较烦人
  • 软工大三开学总结
  • 连接 USB 设备
  • SpringBoot-day1(快速上手SpringBoot,SpringBoot简介,SpringBoot基础配置,属性配置,yaml文件) - a
  • elk time
  • 详细介绍:【OpenHarmony】用户文件服务模块架构
  • 详细介绍:全新 CloudPilot AI:嵌入 Kubernetes 的 SRE Agent,降本与韧性双提升!
  • “环境变量”是什么, 为什么要配置环境变量 --初学者
  • Java 装饰器模式(Decorator) - krt
  • Python configparser 模块 - INI 文件读写利器
  • QT:获取文件信息之创建日期方法created()方法--废弃
  • 排列组合 容斥 总结
  • C/C++ 指针详解与各种指针定义 - 指南
  • 玄机——第一章 应急响应-Linux日志分析 wp
  • 第四周第五天4.5
  • 12 10.11
  • 09 面向对象基础概念的总结
  • CSP-S 2025 提高级模拟赛 Day6 复盘 B.连通子图
  • 基于Java的家政服务管理优秀的系统的设计与完成-计算机毕设 附源码05300
  • 业务定义与指标体系搭建
  • centos7 离线安装mysql8 并建立主从架构