如何实现随机化试题并插入图片?求可用Java代码
随机抽题并支持图片的Java实现方案
1. 定义试题实体类
创建Quiz类封装试题数据,包含题目文本、选项、正确答案索引、图片路径(支持null表示无图片):
import java.util.List; public class Quiz { private String question; private List<String> choices; private int correctAnswerIndex; // 选项索引从0开始 private String imagePath; // 图片本地路径或资源路径 public Quiz(String question, List<String> choices, int correctAnswerIndex, String imagePath) { this.question = question; this.choices = choices; this.correctAnswerIndex = correctAnswerIndex; this.imagePath = imagePath; } // Getter方法 public String getQuestion() { return question; } public List<String> getChoices() { return choices; } public int getCorrectAnswerIndex() { return correctAnswerIndex; } public String getImagePath() { return imagePath; } }
2. 随机抽题核心逻辑
实现随机抽取单题或多道不重复试题的工具类:
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class QuizRandomizer { // 随机抽取单个试题 public static Quiz getRandomQuiz(List<Quiz> quizList) { if (quizList == null || quizList.isEmpty()) { throw new IllegalArgumentException("试题列表不能为空"); } Random random = new Random(); return quizList.get(random.nextInt(quizList.size())); } // 随机抽取指定数量的不重复试题 public static List<Quiz> getRandomQuizzes(List<Quiz> quizList, int count) { if (quizList == null || quizList.isEmpty() || count <= 0 || count > quizList.size()) { throw new IllegalArgumentException("参数不合法"); } List<Quiz> shuffledList = new ArrayList<>(quizList); Collections.shuffle(shuffledList); return shuffledList.subList(0, count); } }
3. 试题展示实现
控制台版本(快速测试用)
import java.util.Arrays; import java.util.List; public class QuizConsoleDemo { public static void main(String[] args) { // 初始化试题库,部分题目带图片路径 List<Quiz> quizBank = Arrays.asList( new Quiz( "What does HTML stand for?", Arrays.asList( "Hyperlinks and Text Markup Language", "Hyper Text Markup Language", "Hyper Text Making Language", "Hyper Text Mark Language" ), 1, "images/html_logo.png" // 假设项目根目录下有此图片 ), new Quiz( "Which HTML tag defines an internal style sheet?", Arrays.asList("<script>", "<style>", "<html>", "<svg>"), 1, null // 无图片 ), new Quiz( "Which property changes background color?", Arrays.asList("backgroundColor", "BgColor", "Color-Background", "background"), 3, "images/css_bg.png" ) ); // 随机抽取1道题并展示 Quiz randomQuiz = QuizRandomizer.getRandomQuiz(quizBank); displayQuiz(randomQuiz); } private static void displayQuiz(Quiz quiz) { System.out.println("题目:" + quiz.getQuestion()); // 显示图片路径(如果有) if (quiz.getImagePath() != null) { System.out.println("图片路径:" + quiz.getImagePath()); } // 打印选项 List<String> choices = quiz.getChoices(); for (int i = 0; i < choices.size(); i++) { System.out.println((i + 1) + ". " + choices.get(i)); } } }
Swing GUI版本(带图片显示)
import javax.swing.*; import java.awt.*; import java.util.Arrays; import java.util.List; public class QuizGuiDemo extends JFrame { public QuizGuiDemo() { setTitle("随机抽题系统"); setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); // 初始化试题库 List<Quiz> quizBank = Arrays.asList( new Quiz( "What does HTML stand for?", Arrays.asList( "Hyperlinks and Text Markup Language", "Hyper Text Markup Language", "Hyper Text Making Language", "Hyper Text Mark Language" ), 1, "images/html_logo.png" ), new Quiz( "Which is the correct CSS syntax?", Arrays.asList("body{color:black}", "{body{color:black}", "body={color:black}", "body:color{black}"), 0, "images/css_syntax.png" ) ); // 随机抽题并展示 Quiz randomQuiz = QuizRandomizer.getRandomQuiz(quizBank); displayQuizInGui(randomQuiz); } private void displayQuizInGui(Quiz quiz) { // 顶部题目面板 JPanel questionPanel = new JPanel(new BorderLayout()); JLabel questionLabel = new JLabel("<html><h3>" + quiz.getQuestion() + "</h3></html>"); questionPanel.add(questionLabel, BorderLayout.NORTH); // 图片显示区域 if (quiz.getImagePath() != null) { ImageIcon imageIcon = new ImageIcon(quiz.getImagePath()); // 缩放图片适应窗口 Image scaledImage = imageIcon.getImage().getScaledInstance(400, 200, Image.SCALE_SMOOTH); JLabel imageLabel = new JLabel(new ImageIcon(scaledImage)); questionPanel.add(imageLabel, BorderLayout.CENTER); } // 选项面板 JPanel choicesPanel = new JPanel(new GridLayout(4, 1)); ButtonGroup group = new ButtonGroup(); for (String choice : quiz.getChoices()) { JRadioButton radioButton = new JRadioButton(choice); group.add(radioButton); choicesPanel.add(radioButton); } add(questionPanel, BorderLayout.NORTH); add(choicesPanel, BorderLayout.CENTER); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new QuizGuiDemo().setVisible(true)); } }
注意事项
- 图片路径需根据实际项目结构调整,可使用绝对路径或项目资源路径
- 若使用Maven/Gradle项目,建议将图片放入
src/main/resources目录,通过getResource()方法获取路径 - 随机抽题逻辑支持不重复抽取,适合生成试卷场景
内容的提问来源于stack exchange,提问作者david boi
相关产品推荐
相关产品推荐

