编写程序以创建多项选择测验 需要一个基于 GUI 的程序,使人们能够从多项选择题库中进行测试。每个问题包括: 问题文本; 4个可能的答案; 正确答案的数量(1-4,而不是0-3); 问题按顺序保存在文本文件中,每个问题保存在 6 行上。问题文本文件中的示例条目可能如下所示: 该计划应具有以下特点: 一个包含“打开”和“退出”的菜单,其中“打开”启动 JFileChooser 以选择包含问题的文件,“退出”结束程序。 加载文件后,GUI 应一次显示一个问题及其答案。 用户应该能够选择一个答案,并且应该通知他们是否正确。 应让用户知道正确回答的数量和回答的问题总数。 用户应该只有在回答了当前问题后才能继续下一个问题。 回答完所有问题后,应通知用户他们的总分以及游戏已完成。现在应启用“打开”菜单项以启动新测验。(可选)您可以添加重新启动菜单项以重做当前测验。
时间: 2024-02-11 21:08:00 浏览: 18
好的,这个问题是关于编写一个基于GUI的程序,使人们能够从多项选择题库中进行测试。下面是一个简单的示例代码,它可以实现上述计划的功能:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class MultipleChoiceQuiz extends JFrame implements ActionListener {
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem openMenuItem;
private JMenuItem exitMenuItem;
private JLabel questionLabel;
private JRadioButton answerButton1;
private JRadioButton answerButton2;
private JRadioButton answerButton3;
private JRadioButton answerButton4;
private JButton nextButton;
private int totalQuestions;
private int currentQuestion;
private int correctAnswers;
private String[] questions;
private String[][] answerChoices;
private int[] correctAnswerNumbers;
public MultipleChoiceQuiz() {
setTitle("多项选择测验");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建菜单栏
menuBar = new JMenuBar();
fileMenu = new JMenu("文件");
openMenuItem = new JMenuItem("打开");
openMenuItem.addActionListener(this);
fileMenu.add(openMenuItem);
exitMenuItem = new JMenuItem("退出");
exitMenuItem.addActionListener(this);
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// 创建问题和答案标签
questionLabel = new JLabel("", JLabel.CENTER);
answerButton1 = new JRadioButton("");
answerButton2 = new JRadioButton("");
answerButton3 = new JRadioButton("");
answerButton4 = new JRadioButton("");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(answerButton1);
buttonGroup.add(answerButton2);
buttonGroup.add(answerButton3);
buttonGroup.add(answerButton4);
nextButton = new JButton("下一个问题");
nextButton.setEnabled(false);
nextButton.addActionListener(this);
// 创建面板并将组件添加到面板上
JPanel quizPanel = new JPanel(new GridLayout(6, 1));
quizPanel.add(questionLabel);
quizPanel.add(answerButton1);
quizPanel.add(answerButton2);
quizPanel.add(answerButton3);
quizPanel.add(answerButton4);
quizPanel.add(nextButton);
add(quizPanel);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == openMenuItem) {
// 打开文件并加载问题和答案
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(this);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
loadQuiz(selectedFile);
showNextQuestion();
}
} else if (e.getSource() == exitMenuItem) {
// 退出程序
System.exit(0);
} else if (e.getSource() == nextButton) {
// 检查答案并显示下一个问题
if (answerButton1.isSelected() || answerButton2.isSelected() || answerButton3.isSelected() || answerButton4.isSelected()) {
checkAnswer();
if (currentQuestion < totalQuestions) {
showNextQuestion();
} else {
showResult();
}
}
}
}
private void loadQuiz(File quizFile) {
try {
Scanner scanner = new Scanner(quizFile);
totalQuestions = Integer.parseInt(scanner.nextLine());
questions = new String[totalQuestions];
answerChoices = new String[totalQuestions][4];
correctAnswerNumbers = new int[totalQuestions];
for (int i = 0; i < totalQuestions; i++) {
questions[i] = scanner.nextLine();
answerChoices[i][0] = scanner.nextLine();
answerChoices[i][1] = scanner.nextLine();
answerChoices[i][2] = scanner.nextLine();
answerChoices[i][3] = scanner.nextLine();
correctAnswerNumbers[i] = Integer.parseInt(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void showNextQuestion() {
// 显示下一个问题和答案
questionLabel.setText(questions[currentQuestion]);
answerButton1.setText(answerChoices[currentQuestion][0]);
answerButton2.setText(answerChoices[currentQuestion][1]);
answerButton3.setText(answerChoices[currentQuestion][2]);
answerButton4.setText(answerChoices[currentQuestion][3]);
answerButton1.setSelected(false);
answerButton2.setSelected(false);
answerButton3.setSelected(false);
answerButton4.setSelected(false);
// 增加当前问题计数器
currentQuestion++;
// 禁用“下一个问题”按钮
nextButton.setEnabled(false);
}
private void checkAnswer() {
// 检查用户选择的答案是否正确
if ((answerButton1.isSelected() && correctAnswerNumbers[currentQuestion - 1] == 1) ||
(answerButton2.isSelected() && correctAnswerNumbers[currentQuestion - 1] == 2) ||
(answerButton3.isSelected() && correctAnswerNumbers[currentQuestion - 1] == 3) ||
(answerButton4.isSelected() && correctAnswerNumbers[currentQuestion - 1] == 4)) {
correctAnswers++;
JOptionPane.showMessageDialog(this, "回答正确!", "答案", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "回答错误。正确答案是 " + getCorrectAnswer() + "。", "答案", JOptionPane.ERROR_MESSAGE);
}
// 启用“下一个问题”按钮
nextButton.setEnabled(true);
}
private String getCorrectAnswer() {
// 获取当前问题的正确答案
if (correctAnswerNumbers[currentQuestion - 1] == 1) {
return answerChoices[currentQuestion - 1][0];
} else if (correctAnswerNumbers[currentQuestion - 1] == 2) {
return answerChoices[currentQuestion - 1][1];
} else if (correctAnswerNumbers[currentQuestion - 1] == 3) {
return answerChoices[currentQuestion - 1][2];
} else {
return answerChoices[currentQuestion - 1][3];
}
}
private void showResult() {
// 显示测验结果
JOptionPane.showMessageDialog(this, "你的得分是:" + correctAnswers + "/" + totalQuestions + "。游戏已完成。", "结果", JOptionPane.INFORMATION_MESSAGE);
// 重置测验
totalQuestions = 0;
currentQuestion = 0;
correctAnswers = 0;
questions = null;
answerChoices = null;
correctAnswerNumbers = null;
// 禁用“下一个问题”按钮
nextButton.setEnabled(false);
}
public static void main(String[] args) {
MultipleChoiceQuiz quiz = new MultipleChoiceQuiz();
quiz.setVisible(true);
}
}
```
上面的代码示例中,我们实现了一个基于GUI的多项选择测验程序。我们使用 JMenuBar、JMenu、JMenuItem、JLabel、JRadioButton、JButton 和 JPanel 等组件来创建用户界面。我们还使用 JFileChooser 来打开包含问题的文件。在加载问题和答案后,我们使用 JLabel 和 JRadioButton 来显示问题和答案选项。我们还使用 JButton 来允许用户回答问题,并使用 JOptionPane 来通知用户答案是否正确。在回答完所有问题后,我们使用 JOptionPane 显示用户的总分,并重置测验。