9.18日

发布时间 2023-09-18 22:45:15作者: yblll
今天早上进行了传统制造实训,简单了解到车床、洗床、数控机床等等的操作方法及原理,下午简单学习了方法论,简单写了个四则运算的小代码。如下:
package lianxi1;

import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

public class test01 {
    private static final int TIME_LIMIT = 10; // 设置测试时间限制为60秒
    private static boolean timedOut = false; // 超时标志

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        int correctAnswers = 0;
        int incorrectAnswers = 0;

        Set<String> questions = new HashSet<>();

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                timedOut = true; 
            }
        }, TIME_LIMIT * 1000);

        while (questions.size() < 30 && !timedOut) {
            int operand1 = generateOperand(random);
            int operand2 = generateOperand(random);
            int operator = generateOperator(random);

            int result = calculateResult(operand1, operand2, operator);

            if (result > 1000 || result < 0) {
                continue;
            }

            String question = operand1 + " " + getOperatorSymbol(operator) + " " + operand2;
            if (questions.contains(question)) {
                continue;
            }

            questions.add(question);

            System.out.print("题目" + questions.size() + ": ");
            System.out.print(question + " = ");

            int userAnswer;
            if (!timedOut) {
                userAnswer = getUserAnswer(scanner);
            } else {
                userAnswer = -1; // 超时未回答问题,将答案设置为-1
            }

            if (userAnswer == result) {
                System.out.println("正确!");
                correctAnswers++;
            } else {
                System.out.println("错误! 正确答案是 " + result);
                incorrectAnswers++;
            }
        }

        printResult(correctAnswers, incorrectAnswers);

        timer.cancel(); 

        scanner.close();
    }

    

    public static int generateOperand(Random random) {
        return random.nextInt(80) + 1;
    }

    public static int generateOperator(Random random) {
        return random.nextInt(4); 
    }

    public static char getOperatorSymbol(int operator) {
        char operatorSymbol;
        switch (operator) {
            case 0:
                operatorSymbol = '+';
                break;
            case 1:
                operatorSymbol = '-';
                break;
            case 2:
                operatorSymbol = '*';
                break;
            case 3:
                operatorSymbol = '/';
                break;
            default:
                operatorSymbol = '+';
                break;
        }
        return operatorSymbol;
    }

    public static int calculateResult(int operand1, int operand2, int operator) {
        int result;
        switch (operator) {
            case 0:
                result = operand1 + operand2;
                break;
            case 1:
                result = operand1 - operand2;
                break;
            case 2:
                result = operand1 * operand2;
                break;
            case 3:
                if (operand2 == 0) {
                    result = 0;
                } else {
                     int quotient = operand1 / operand2;
                     int remainder = operand1 % operand2;
                     if (remainder != 0) {
                         operand1 = operand2 * quotient;
                     }
                     result = operand1 / operand2;
                }
                break;
            default:
                result = operand1 + operand2;
                break;
        }
        return result;
    }

    public static int getUserAnswer(Scanner scanner) {
        int userAnswer;
        while (true) {
            try {
                userAnswer = scanner.nextInt();
                break;
            } catch (Exception e) {
                System.out.println("输入错误. 请重新输入整数!");
                scanner.nextLine();
            }
        }
        return userAnswer;
    }

    public static void printResult(int correctAnswers, int incorrectAnswers) {
        System.out.println("答题结束!");
        System.out.println("正确题数: " + correctAnswers);
        System.out.println("错题数: " + incorrectAnswers);
        double accuracy = (double) correctAnswers / (correctAnswers + incorrectAnswers) * 100;
        System.out.println("正确率: " + accuracy + "%");
        
        if (timedOut) {
            System.out.println("倒计时结束,在规定时间内未完成测试题!");
    }
    }
}