java写一个试题库管理系统
时间: 2023-12-20 15:07:26 浏览: 85
下面是一个简单的Java试题库管理系统的代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class QuestionBank {
private ArrayList<Question> questions = new ArrayList<>();
public void addQuestion() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入题目类型(单选题、多选题、判断题):");
String type = scanner.next();
System.out.print("请输入题目描述:");
String description = scanner.next();
System.out.print("请输入选项(每个选项用逗号隔开):");
String[] options = scanner.next().split(",");
System.out.print("请输入正确答案(单选题和判断题只需输入一个选项编号,多选题需要输入多个选项编号,用逗号隔开):");
String[] answer = scanner.next().split(",");
Question question = new Question(type, description, options, answer);
questions.add(question);
System.out.println("题目已添加成功!");
}
public void deleteQuestion() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的题目编号:");
int index = scanner.nextInt() - 1;
if (index < 0 || index >= questions.size()) {
System.out.println("题目编号无效!");
return;
}
questions.remove(index);
System.out.println("题目已删除成功!");
}
public void queryQuestion() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查询的题目类型(单选题、多选题、判断题),如果不限制类型则输入all:");
String type = scanner.next();
System.out.print("请输入要查询的关键字:");
String keyword = scanner.next();
System.out.println("查询结果如下:");
int count = 0;
for (Question question : questions) {
if (type.equals("all") || question.getType().equals(type)) {
if (question.getDescription().contains(keyword)) {
count++;
System.out.println("题目编号:" + (count) + ",题目类型:" + question.getType() + ",题目描述:" + question.getDescription() + ",选项:" + String.join(",", question.getOptions()) + ",答案:" + String.join(",", question.getAnswer()));
}
}
}
if (count == 0) {
System.out.println("没有查询到任何题目!");
}
}
public void showMenu() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加题目");
System.out.println("2. 删除题目");
System.out.println("3. 查询题目");
System.out.println("4. 退出程序");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addQuestion();
break;
case 2:
deleteQuestion();
break;
case 3:
queryQuestion();
break;
case 4:
System.out.println("程序已退出!");
return;
default:
System.out.println("无效的选项,请重新输入!");
break;
}
}
}
public static void main(String[] args) {
QuestionBank questionBank = new QuestionBank();
questionBank.showMenu();
}
}
class Question {
private String type;
private String description;
private String[] options;
private String[] answer;
public Question(String type, String description, String[] options, String[] answer) {
this.type = type;
this.description = description;
this.options = options;
this.answer = answer;
}
public String getType() {
return type;
}
public String getDescription() {
return description;
}
public String[] getOptions() {
return options;
}
public String[] getAnswer() {
return answer;
}
}
```
这个试题库管理系统包含了添加题目、删除题目、查询题目等功能。当用户选择添加题目时,程序会要求用户输入题目类型、题目描述、选项和答案,并将题目添加到题库中;当用户选择删除题目时,程序会要求用户输入要删除的题目编号,并将该题目从题库中删除;当用户选择查询题目时,程序会要求用户输入题目类型和关键字,然后根据用户输入的条件在题库中进行查询,并将查询结果输出到屏幕上。
在实际的应用中,可以根据需要对程序进行进一步的修改和完善,例如添加登录界面、界面美化等功能。
阅读全文