You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java测验游戏中实现题目随机抽取?

实现Java测验游戏的随机出题功能

基础方案:用数组直接绑定题目内容

你已经掌握了Random类的基础用法,只需要把题目内容和数组关联,通过随机索引就能直接获取对应题目。比如用字符串数组存储题目:

import java.util.Random;

public class QuizGame {
    public static void main(String[] args) {
        // 存储4道测验题目
        String[] questions = {
            "题目1:Java的基本数据类型有哪些?",
            "题目2:ArrayList和LinkedList的核心区别是什么?",
            "题目3:面向对象的三大特性指的是什么?",
            "题目4:try-catch-finally代码块的执行顺序是怎样的?"
        };

        Random rand = new Random();
        // 生成0到数组长度-1的随机索引
        int randomIndex = rand.nextInt(questions.length);
        // 根据索引取出对应的随机题目
        String randomQuestion = questions[randomIndex];
        
        System.out.println("随机抽取的题目:" + randomQuestion);
    }
}

如果需要同时关联题目编号和内容,可以用二维数组实现:

String[][] quizItems = {
    {"1", "题目1的具体内容..."},
    {"2", "题目2的具体内容..."},
    {"3", "题目3的具体内容..."},
    {"4", "题目4的具体内容..."}
};

// 随机选取题目
Random rand = new Random();
int randomIndex = rand.nextInt(quizItems.length);
System.out.println("题目编号:" + quizItems[randomIndex][0] + ",题目内容:" + quizItems[randomIndex][1]);

进阶方案:自定义Question类(适配复杂题目)

如果你的题目包含题干、选项、答案等多类信息,建议自定义一个Question类,用数组或List存储题目对象,扩展性更强:

import java.util.Random;

// 自定义题目类,封装题目相关信息
class Question {
    private int id;
    private String content;
    private String[] options;
    private String answer;

    // 构造方法
    public Question(int id, String content, String[] options, String answer) {
        this.id = id;
        this.content = content;
        this.options = options;
        this.answer = answer;
    }

    // 获取题目信息的getter方法
    public int getId() { return id; }
    public String getContent() { return content; }
    public String[] getOptions() { return options; }
    public String getAnswer() { return answer; }
}

public class QuizGame {
    public static void main(String[] args) {
        // 初始化题目数组
        Question[] questions = {
            new Question(1, "Java中int类型的取值范围?", new String[]{"A. -2^15到2^15-1", "B. -2^31到2^31-1", "C. 0到2^32-1"}, "B"),
            new Question(2, "以下哪个是Java的关键字?", new String[]{"A. main", "B. String", "C. static"}, "C"),
            new Question(3, "多态的核心定义是?", new String[]{"A. 一个类包含多个方法", "B. 子类重写父类方法", "C. 同一行为的不同实现"}, "C"),
            new Question(4, "Java垃圾回收的主要作用是?", new String[]{"A. 释放未使用的内存", "B. 提升代码运行速度", "C. 清理磁盘文件"}, "A")
        };

        Random rand = new Random();
        int randomIndex = rand.nextInt(questions.length);
        Question selectedQuestion = questions[randomIndex];

        // 输出完整题目信息
        System.out.println("题目编号:" + selectedQuestion.getId());
        System.out.println("题目内容:" + selectedQuestion.getContent());
        System.out.println("选项:");
        for (String option : selectedQuestion.getOptions()) {
            System.out.println(option);
        }
    }
}

可选需求:避免重复出题

如果希望每次抽取的题目不重复,可以把题目放到ArrayList中,每次随机取出后移除该题目:

import java.util.ArrayList;
import java.util.Random;

public class QuizGame {
    public static void main(String[] args) {
        ArrayList<String> questionList = new ArrayList<>();
        questionList.add("题目1...");
        questionList.add("题目2...");
        questionList.add("题目3...");
        questionList.add("题目4...");

        Random rand = new Random();
        // 列表不为空时持续出题
        while (!questionList.isEmpty()) {
            int randomIndex = rand.nextInt(questionList.size());
            String selectedQuestion = questionList.remove(randomIndex);
            System.out.println("当前题目:" + selectedQuestion);
        }
    }
}

内容的提问来源于stack exchange,提问作者Gabe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 18:02:18