java在线考试系统编程实例
时间: 2023-12-27 09:04:06 浏览: 85
很抱歉,根据提供的引用内容,没有提供具体的Java在线考试系统的编程实例。但是,我可以为您提供一个基本的Java在线考试系统的编程框架,供您参考:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Question {
private String question;
private List<String> options;
private int correctOption;
public Question(String question, List<String> options, int correctOption) {
this.question = question;
this.options = options;
this.correctOption = correctOption;
}
public String getQuestion() {
return question;
}
public List<String> getOptions() {
return options;
}
public int getCorrectOption() {
return correctOption;
}
}
class Exam {
private List<Question> questions;
public Exam(List<Question> questions) {
this.questions = questions;
}
public void startExam() {
int score = 0;
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < questions.size(); i++) {
Question question = questions.get(i);
System.out.println("Question " + (i + 1) + ": " + question.getQuestion());
List<String> options = question.getOptions();
for (int j = 0; j < options.size(); j++) {
System.out.println((j + 1) + ". " + options.get(j));
}
System.out.print("Enter your answer: ");
int answer = scanner.nextInt();
if (answer == question.getCorrectOption()) {
score++;
}
}
System.out.println("Your score: " + score + "/" + questions.size());
}
}
public class OnlineExamSystem {
public static void main(String[] args) {
List<Question> questions = new ArrayList<>();
questions.add(new Question("What is the capital of France?", List.of("London", "Paris", "Berlin", "Madrid"), 2));
questions.add(new Question("What is the largest planet in our solar system?", List.of("Mars", "Jupiter", "Earth", "Saturn"), 2));
questions.add(new Question("What is the chemical symbol for gold?", List.of("Au", "Ag", "Fe", "Cu"), 1));
Exam exam = new Exam(questions);
exam.startExam();
}
}
```
这是一个简单的Java在线考试系统的编程示例。它包括了问题类(Question)和考试类(Exam),并在主类(OnlineExamSystem)中创建了一些问题并开始考试。您可以根据自己的需求进行扩展和修改。
阅读全文