首页 > 其他分享 >四则运算2

四则运算2

时间:2024-09-30 16:35:53浏览次数:8  
标签:num1 num2 int 四则运算 question static answer

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

public class ArithmeticQuiz {
private static final int NUMBER_OF_QUESTIONS = 10;
private static final int TIME_LIMIT_PER_QUESTION = 30; // seconds
private static List questions = new ArrayList<>();
private static int correctAnswers = 0;
private static int wrongAnswers = 0;
private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    generateQuestions();
    startQuiz();
    showResults();
}

private static void generateQuestions() {
    Set<String> uniqueQuestions = new HashSet<>();
    Random random = new Random();
    
    while (questions.size() < NUMBER_OF_QUESTIONS) {
        int type = random.nextInt(4); // 0: +, 1: -, 2: *, 3: /
        int num1 = random.nextInt(100) + 1; // 1 to 100
        int num2 = random.nextInt(100) + 1; // 1 to 100
        int answer = 0;
        String questionText = "";
        
        switch (type) {
            case 0: // Addition
                answer = num1 + num2;
                questionText = num1 + " + " + num2;
                break;
            case 1: // Subtraction
                if (num1 >= num2) {
                    answer = num1 - num2;
                    questionText = num1 + " - " + num2;
                }
                break;
            case 2: // Multiplication
                answer = num1 * num2;
                if (answer < 10000) {
                    questionText = num1 + " * " + num2;
                }
                break;
            case 3: // Division
                if (num2 != 0 && num1 % num2 == 0) {
                    answer = num1 / num2;
                    questionText = num1 + " / " + num2;
                }
                break;
        }
        
        if (!questionText.isEmpty() && uniqueQuestions.add(questionText)) {
            questions.add(new ArithmeticQuestion(questionText, answer));
        }
    }
}

private static void startQuiz() {
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    
    for (ArithmeticQuestion question : questions) {
        System.out.println("Question: " + question.question);
        executor.schedule(() -> {
            System.out.println("Time's up! Moving to the next question.");
            wrongAnswers++;
            askQuestion(false, question); 
        }, TIME_LIMIT_PER_QUESTION, TimeUnit.SECONDS);
        
        boolean answered = askQuestion(true, question);
        if (answered) {
            executor.shutdownNow();
            executor = Executors.newScheduledThreadPool(1); // Create a new executor for the next question
        }
    }
    
    executor.shutdownNow();
}

private static boolean askQuestion(boolean waitForAnswer, ArithmeticQuestion question) {
    if (waitForAnswer) {
        System.out.print("Your answer: ");
        int userAnswer = scanner.nextInt();
        if (userAnswer == question.answer) {
            correctAnswers++;
            System.out.println("Correct!");
        } else {
            wrongAnswers++;
            System.out.println("Wrong! Correct answer was: " + question.answer);
        }
        return true; // User has answered
    }
    return false; // Time's up, user didn't answer
}

private static void showResults() {
    System.out.println("\nQuiz Results:");
    System.out.println("Correct Answers: " + correctAnswers);
    System.out.println("Wrong Answers: " + wrongAnswers);
    double accuracy = ((double) correctAnswers / (correctAnswers + wrongAnswers)) * 100;
    System.out.printf("Accuracy: %.2f%%\n", accuracy);
}

}

class ArithmeticQuestion {
String question;
int answer;

ArithmeticQuestion(String question, int answer) {
    this.question = question;
    this.answer = answer;
}

}

标签:num1,num2,int,四则运算,question,static,answer
From: https://www.cnblogs.com/bixi/p/18442083

相关文章

  • 结对项目——实现一个自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/这个作业要求在哪里结对项目-作业-计科22级34班-班级博客-博客园(cnblogs.com)这个作业的目标结对项目——实现一个自动生成小学四则运算题目的命令行程序成员1陈奕奕32220045......
  • 结对项目:自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程计科22级12班这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-12/homework/13221姓名学号曾繁曦3122004841吴健民3122004667PSP表格一、流程图二、模块设计1.模块划分Main模块(Main.java)功能描述:程序的入......
  • 实现一个自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程计科22级12班这个作业要求在哪里结对项目这个作业的目标实现一个自动生成小学四则运算题目的命令行程序。一:詹洛熙3122004800GitHub链接(https://github.com/Zhannoi/-/tree/main)二:PSP表格PSP2.1PersonalSoftwarePr......
  • 结对项目——四则运算
    结对项目——四则运算这个作业属于哪个课程软工22级计科12班这个作业的要求在哪里作业要求这个作业的目标实现四则运算的结对编程项目成员姓名学号GitHub链接分工谭立业3122004365github项目功能的基本实现,博客的编写罗锴佳3122001905gith......
  • 实现一个自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标通过实现一个自动生成小学四则运算题目的命令行程序提高软件开发能力姓名李佳聪......
  • 基于python的四则运算自动生成的命令行程序
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标基于python的四则运算自动生成的命令行程序团队成员姚彬彬3122006324一.Githu......
  • 结对项目:自动生成小学四则运算题目
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标结对实现一个自动生成小学四则运算题目的命令行程序项目一、项目开发人员以及仓库地......
  • 结对项目——小学四则运算题目自动生成器
    这个作业属于哪个课程<计科22级34班>这个作业要求在哪里<结对项目>这个作业的目标<实现一个自动生成小学四则运算题目的命令行程序(也可以用图像界面,具有相似功能)>团队成员<杨富国(3122004587)、李思柔(3222004638)>Github项目地址https://github.com/wWchao-111......
  • 四则运算
    packagechuti;importjava.util.Scanner;publicclassDemo{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);for(inti=1;i<=30;i++){inta=(int)Math.round(Math.random()*100);intb=(int)Math.round(Math.rando......
  • 结对项目--四则运算
    结对项目--四则运算这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标四则运算成员3122004564方伟城psp表格PSP2.1Personal......