实验与思考作业2

import java.util.ArrayList;
import java.util.List;
public class AnswerCalculator {
    // 计算题目的答案
    public List<Integer> calculateAnswers(List<String> questions) {
        List<Integer> answers = new ArrayList<>();
        for (String question : questions) {
            String[] parts = question.split(" ");
            int operand1 = Integer.parseInt(parts[0]);
            int operand2 = Integer.parseInt(parts[2]);
            String operator = parts[1];
            int answer = 0;
            switch (operator) {
                case "+":
                    answer = operand1 + operand2;
                    break;
                case "-":
                    if (operand1 < operand2) {
                        return null; // 减法不允许出现负数,返回null表示无法计算答案
                    }
                    answer = operand1 - operand2;
                    break;
                case "*":
                    answer = operand1 * operand2;
                    break;
                case "/":
                    if (operand2 != 0 && operand1 % operand2 == 0) {
                        answer = operand1 / operand2;
                    }
                    else {
                        return null; // 除法无法整除,返回null表示无法计算答案
                    }
                    break;
            }
            answers.add(answer);
        }
        return answers;
    }
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.HashSet;
import java.util.Set;
public class QuestionGenerator {
    // 题目不重复的生成和判断
    public List<String> generateQuestions(int totalQuestions) {
        List<String> questions = new ArrayList<>();
        Set<String> questionSet = new HashSet<>(); // 使用HashSet来存储已生成的题目
        Random random = new Random();
        while (questions.size() < totalQuestions) {
            int operand1 = random.nextInt(100)+1;
            int operand2 = random.nextInt(100)+1;
            String operator = getRandomOperator();
            String question = operand1 + " " + operator + " " + operand2;
            if (!questions.contains(question) && isValidQuestion(question)) {
                questions.add(question);
                questionSet.add(question); // 将新生成的题目添加到Set中
            }
        }
        return questions;
    }
    // 随机生成运算符
    private String getRandomOperator() {
        String[] operators = {"+", "-", "*", "/"};
        Random random = new Random();
        int index = random.nextInt(operators.length);
        return operators[index];
    }
    private boolean isValidQuestion(String question) {
        String[] parts = question.split(" ");
        int operand1 = Integer.parseInt(parts[0]);
        int operand2 = Integer.parseInt(parts[2]);
        String operator = parts[1];
        int answer = 0;
        switch (operator) {
            case "+":
                answer = operand1 + operand2;
                break;
            case "-":
                if (operand1 < operand2) {
                return false;
                }
                answer = operand1 - operand2;
                break;
            case "*":
                answer = operand1 * operand2;
                break;
            case "/":
                if (operand2 != 0 && operand1 % operand2 == 0) {
                    answer = operand1 / operand2;
                }
                else {
                    return false; // 除法无法整除,不符合要求
                }
                break;
        }
        return answer >= 0 && answer <= 999; // 乘法结果不能超过3位数
    }
}
import java.util.List;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int totalQuestions = 30; // 题目数量
        int correctAnswers = 0; // 正确答案数量
        QuestionGenerator generator = new QuestionGenerator();
        List<String> questions = generator.generateQuestions(totalQuestions); // 生成题目列表
        AnswerCalculator calculator = new AnswerCalculator();
        List<Integer> answers = calculator.calculateAnswers(questions); // 计算题目的答案
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入答题时间(单位:秒):");
        int timeLimit = scanner.nextInt();
        long startTime = System.currentTimeMillis();
        // 开始答题
        for (int i = 0; i < questions.size(); i++) {
            String question = questions.get(i);
            System.out.println(question);
            // 检查时间限制
            long currentTime = System.currentTimeMillis();
            int elapsedTime = (int) ((currentTime - startTime) / 1000);
            if (elapsedTime >= timeLimit) {
                System.out.println("时间到!答题结束。");
                break;
            }
            int userAnswer = scanner.nextInt();
            int correctAnswer = answers.get(i);
            if (userAnswer == correctAnswer) {
                System.out.println("回答正确!");
                correctAnswers++;
            } else {
                System.out.println("回答错误!正确答案是:" + correctAnswer);
            }
        }
        scanner.close();
        // 统计正确率
        double accuracy = (double) correctAnswers / totalQuestions * 100;
        System.out.println("答题结束,正确率为:" + accuracy + "%");
    }
}
 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号