java语言实现选择、判断题目考试系统,要求程序从文件系统(或数据库)中读入考试题目(主要为选择题)及答案,显示在GUI界面中,答题者按照题目内容进行答题,点击按钮提交后可以看到自己的答题结果。 界面要示:图形界面实现。具体代码请给出
时间: 2023-12-14 10:36:00 浏览: 50
以下是一个简单的Java语言实现选择、判断题目考试系统的示例代码:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ExamSystem extends JFrame implements ActionListener {
private JLabel questionLabel;
private JRadioButton[] answerButtons;
private JButton submitButton;
private int currentQuestionIndex;
private int score;
private ExamQuestion[] questions = new ExamQuestion[] {
new ExamQuestion("Which of the following is not a programming language?", new String[] { "Java", "Python", "HTML", "C++" }, 2),
new ExamQuestion("What is the output of the following code?\nint x = 5;\nSystem.out.println(x++);\nSystem.out.println(x);", new String[] { "5 5", "5 6", "6 6", "6 5" }, 1),
new ExamQuestion("Which of the following is a valid identifier in Java?", new String[] { "1variable", "_variable", "variable$", "variable#" }, 1),
new ExamQuestion("What is the result of 9 % 4?", new String[] { "2", "3", "4", "1" }, 3),
new ExamQuestion("What is the output of the following code?\nString s1 = \"hello\";\nString s2 = \"world\";\nSystem.out.println(s1 + s2);", new String[] { "helloworld", "hello world", "hello+world", "hello,world" }, 0)
};
public ExamSystem() {
super("Exam System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
questionLabel = new JLabel("Question 1: " + questions[0].getQuestion());
answerButtons = new JRadioButton[4];
for (int i = 0; i < 4; i++) {
answerButtons[i] = new JRadioButton(questions[0].getAnswers()[i]);
}
submitButton = new JButton("Submit");
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel questionPanel = new JPanel(new BorderLayout());
questionPanel.add(questionLabel, BorderLayout.NORTH);
JPanel answerPanel = new JPanel(new GridLayout(4, 1));
ButtonGroup answerGroup = new ButtonGroup();
for (int i = 0; i < 4; i++) {
answerPanel.add(answerButtons[i]);
answerGroup.add(answerButtons[i]);
}
questionPanel.add(answerPanel, BorderLayout.CENTER);
mainPanel.add(questionPanel, BorderLayout.CENTER);
mainPanel.add(submitButton, BorderLayout.SOUTH);
setContentPane(mainPanel);
pack();
setLocationRelativeTo(null);
submitButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
int selectedAnswer = -1;
for (int i = 0; i < 4; i++) {
if (answerButtons[i].isSelected()) {
selectedAnswer = i;
break;
}
}
if (selectedAnswer == -1) {
JOptionPane.showMessageDialog(this, "Please select an answer.");
return;
}
if (selectedAnswer == questions[currentQuestionIndex].getCorrectAnswer()) {
score++;
}
currentQuestionIndex++;
if (currentQuestionIndex >= questions.length) {
JOptionPane.showMessageDialog(this, "Your score is: " + score + "/" + questions.length);
System.exit(0);
}
questionLabel.setText("Question " + (currentQuestionIndex + 1) + ": " + questions[currentQuestionIndex].getQuestion());
String[] answers = questions[currentQuestionIndex].getAnswers();
for (int i = 0; i < 4; i++) {
answerButtons[i].setText(answers[i]);
answerButtons[i].setSelected(false);
}
}
public static void main(String[] args) {
ExamSystem examSystem = new ExamSystem();
examSystem.setVisible(true);
}
}
class ExamQuestion {
private String question;
private String[] answers;
private int correctAnswer;
public ExamQuestion(String question, String[] answers, int correctAnswer) {
this.question = question;
this.answers = answers;
this.correctAnswer = correctAnswer;
}
public String getQuestion() {
return question;
}
public String[] getAnswers() {
return answers;
}
public int getCorrectAnswer() {
return correctAnswer;
}
}
```
这个程序使用了Java Swing库来实现图形界面,同时通过一个ExamQuestion类来表示每个考试题目,包括题目内容、选项以及正确答案。程序读取题目数据可以通过从文件系统或数据库中读取来实现。