温馨提示:这篇文章已超过239天没有更新,请注意相关的内容是否还可用!
Java考试系统是一个用于管理和评估学生考试成绩的系统。它可以帮助教师创建考试题目、批改学生答卷并生成成绩报告。以下是一个简单的Java考试系统的代码示例:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExamSystem {
private Map<String, List<String>> examQuestions;
public ExamSystem() {
examQuestions = new HashMap<>();
}
public void addQuestion(String category, String question) {
List<String> questions = examQuestions.getOrDefault(category, new ArrayList<>());
questions.add(question);
examQuestions.put(category, questions);
}
public void removeQuestion(String category, String question) {
List<String> questions = examQuestions.get(category);
if (questions != null) {
questions.remove(question);
if (questions.isEmpty()) {
examQuestions.remove(category);
}
}
}
public void displayQuestions() {
for (String category : examQuestions.keySet()) {
System.out.println("Category: " + category);
List<String> questions = examQuestions.get(category);
for (String question : questions) {
System.out.println(question);
}
System.out.println();
}
}
public static void main(String[] args) {
ExamSystem examSystem = new ExamSystem();
// 添加考试题目
examSystem.addQuestion("Math", "What is 2 + 2?");
examSystem.addQuestion("Math", "Solve for x: 2x + 5 = 15");
examSystem.addQuestion("English", "What is the capital of France?");
examSystem.addQuestion("English", "Who wrote Romeo and Juliet?");
// 移除考试题目
examSystem.removeQuestion("Math", "What is 2 + 2?");
// 显示考试题目
examSystem.displayQuestions();
}
}
上述代码演示了一个简单的Java考试系统。它使用`Map<String, List<String>>`来存储考试题目,其中键为题目的分类,值为该分类下的所有题目。`addQuestion`方法用于添加题目,`removeQuestion`方法用于移除题目,`displayQuestions`方法用于显示所有题目。在示例代码中,我们添加了数学和英语两个分类的题目,并移除了一个数学题目。我们调用`displayQuestions`方法显示所有题目。
这个示例代码只是一个简单的演示,实际的考试系统可能还需要其他功能,如学生答卷的管理和成绩的计算等。但通过这个示例,你可以了解到如何使用Java代码来实现一个简单的考试系统。