作业2

动手动脑:
编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。

点击查看代码
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomDemo {
    /**
     * 生成指定数量随机整数
     * @param count 数目,如1000
     * @param min 最小值
     * @param max 最大值
     * @return 随机整数集合
     */
    public static List<Integer> generateRandomInts(int count, int min, int max) {
        List<Integer> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < count; i++) {
            // nextInt(区间长度) + min
            int num = random.nextInt(max - min + 1) + min;
            list.add(num);
        }
        return list;
    }

    public static void main(String[] args) {
        // 生成1000个随机整数,范围0~9999
        List<Integer> nums = generateRandomInts(1000, 0, 9999);
        System.out.println("生成数量:" + nums.size());
        System.out.println("前10个:" + nums.subList(0,10));
    }
}

四则运算课堂测试一
点击查看代码
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class MathQuiz {

    // 题目内部类,用于存储题目字符串和正确答案
    static class Question {
        String text;
        int answer;

        public Question(String text, int answer) {
            this.text = text;
            this.answer = answer;
        }
    }

    public static void main(String[] args) {
        int totalQuestions = 30; // 题目数量
        int timeLimitSeconds = 120; // 限时时间(秒),对应图片中的倒计时

        List<Question> questions = new ArrayList<>();
        Set<String> questionSet = new HashSet<>(); // 用于去重
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);

        System.out.println("正在生成题目,请稍候...");

        // 阶段1 & 阶段2(1): 生成题目并满足各种限制条件
        while (questions.size() < totalQuestions) {
            int a, b, answer;
            char operator;
            
            // 随机选择运算符 (0:+, 1:-, 2:*, 3:/)
            int opType = random.nextInt(4);
            
            // 控制数字范围,防止结果过大
            if (opType == 0 || opType == 1) { 
                // 加减法数字范围 1-100
                a = random.nextInt(100) + 1;
                b = random.nextInt(100) + 1;
            } else { 
                // 乘除法数字范围 1-20 (避免乘法结果轻松破千)
                a = random.nextInt(20) + 1;
                b = random.nextInt(20) + 1;
            }

            switch (opType) {
                case 0: // 加法
                    operator = '+';
                    answer = a + b;
                    break;
                case 1: // 减法:避免负数
                    operator = '-';
                    if (a < b) { int temp = a; a = b; b = temp; } // 交换,保证大减小
                    answer = a - b;
                    break;
                case 2: // 乘法:结果不允许出现四位数 (即必须 < 1000)
                    operator = '*';
                    if (a * b >= 1000) continue; // 超过三位数,重新生成
                    answer = a * b;
                    break;
                case 3: // 除法:必须整除
                    operator = '/';
                    if (b == 0 || a % b != 0) continue; // 不能整除,重新生成
                    answer = a / b;
                    break;
                default:
                    continue;
            }

            String questionText = a + " " + operator + " " + b + " = ";
            
            // 避免题目重复
            if (!questionSet.contains(questionText)) {
                questionSet.add(questionText);
                questions.add(new Question(questionText, answer));
            }
        }

        // 开始测试
        System.out.println("\n=== 小学生四则运算测试 ===");
        System.out.println("共 " + totalQuestions + " 题,限时 " + timeLimitSeconds + " 秒。");
        System.out.println("请输入答案并按回车。超时未完成视为错题。\n");

        long startTime = System.currentTimeMillis();
        int correctCount = 0;
        int wrongCount = 0;
        boolean isTimeout = false;

        // 阶段2(2) & 阶段2(3): 实时答题与倒计时
        for (int i = 0; i < questions.size(); i++) {
            // 检查是否超时
            long elapsedTime = (System.currentTimeMillis() - startTime) / 1000;
            if (elapsedTime >= timeLimitSeconds) {
                System.out.println("\n[系统提示] 时间到!停止答题。");
                isTimeout = true;
                // 剩余未做的题目全部视为错题
                wrongCount += (totalQuestions - i);
                break;
            }

            Question q = questions.get(i);
            System.out.print("第 " + (i + 1) + " 题: " + q.text);

            // 获取用户输入
            String inputStr = scanner.nextLine().trim();
            int userAnswer;
            
            try {
                userAnswer = Integer.parseInt(inputStr);
            } catch (NumberFormatException e) {
                userAnswer = Integer.MIN_VALUE; // 输入非法字符,视为错误
            }

            // 判断对错
            if (userAnswer == q.answer) {
                correctCount++;
                System.out.println("  √ 正确");
            } else {
                wrongCount++;
                System.out.println("  × 错误 (正确答案: " + q.answer + ")");
            }
        }

        if (!isTimeout) {
            System.out.println("\n[系统提示] 答题结束。");
        }

        // 统计结果
        long totalTimeUsed = (System.currentTimeMillis() - startTime) / 1000;
        double accuracy = ((double) correctCount / totalQuestions) * 100;

        System.out.println("\n==============================");
        System.out.println("          测 试 结 果");
        System.out.println("==============================");
        System.out.println("总题数: " + totalQuestions);
        System.out.println("答对题数: " + correctCount);
        System.out.println("错题/未作数: " + wrongCount);
        System.out.printf("正确率: %.2f%%\n", accuracy);
        System.out.println("本次用时: " + totalTimeUsed + " 秒");
        System.out.println("==============================");

        scanner.close();
    }
}
posted @ 2026-10-08 13:11  宁层层  阅读(9)  评论(0)    收藏  举报