用Java编写一个题库管理系统,要求录入题目功能中的题目录入一道采用文件的形式保存,且功能为1.列出所有试题信息,2.按科目查询 ,3.添加试题,4.删除试题,5.退出系统。界面在控制台输出,写出源码
时间: 2024-03-06 17:48:50 浏览: 125
基于Java题库管理系统.zip
好的,下面是一个Java题库管理系统的源码,实现了列出所有试题信息、按科目查询、添加试题、删除试题、退出系统的功能:
Question类:
```java
public class Question {
private String subject; // 科目
private String content; // 题目内容
private String[] options; // 选项
private int answer; // 答案
public Question(String subject, String content, String[] options, int answer) {
this.subject = subject;
this.content = content;
this.options = options;
this.answer = answer;
}
public String getSubject() {
return subject;
}
public String getContent() {
return content;
}
public String[] getOptions() {
return options;
}
public int getAnswer() {
return answer;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Subject: ").append(subject).append("\n");
sb.append(content).append("\n");
for (int i = 0; i < options.length; i++) {
sb.append((char)('A' + i)).append(". ").append(options[i]).append("\n");
}
sb.append("Answer: ").append((char)('A' + answer));
return sb.toString();
}
}
```
QuestionManager类:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class QuestionManager {
private static final String FILE_NAME = "questions.txt"; // 题目文件名
private List<Question> questions; // 题库
public QuestionManager() {
questions = new ArrayList<>();
loadQuestionsFromFile();
}
// 从文件中加载所有题目
private void loadQuestionsFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
String subject = parts[0];
String content = parts[1];
String[] options = {parts[2], parts[3], parts[4], parts[5]};
int answer = Integer.parseInt(parts[6]);
Question question = new Question(subject, content, options, answer);
questions.add(question);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 保存题目到文件中
private void saveQuestionsToFile() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {
for (Question question : questions) {
writer.write(question.getSubject() + ","
+ question.getContent() + ","
+ question.getOptions()[0] + ","
+ question.getOptions()[1] + ","
+ question.getOptions()[2] + ","
+ question.getOptions()[3] + ","
+ question.getAnswer());
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 列出所有试题信息
public void listAllQuestions() {
for (Question question : questions) {
System.out.println(question);
System.out.println("--------------------");
}
}
// 按科目查询
public void searchBySubject(String subject) {
for (Question question : questions) {
if (question.getSubject().equals(subject)) {
System.out.println(question);
System.out.println("--------------------");
}
}
}
// 添加试题
public void addQuestion() {
Scanner scanner = new Scanner(System.in);
System.out.print("Subject: ");
String subject = scanner.nextLine();
System.out.print("Content: ");
String content = scanner.nextLine();
String[] options = new String[4];
for (int i = 0; i < 4; i++) {
System.out.print((char)('A' + i) + ": ");
options[i] = scanner.nextLine();
}
System.out.print("Answer: ");
int answer = scanner.nextLine().charAt(0) - 'A';
Question question = new Question(subject, content, options, answer);
questions.add(question);
saveQuestionsToFile();
}
// 删除试题
public void deleteQuestion() {
Scanner scanner = new Scanner(System.in);
System.out.print("Subject: ");
String subject = scanner.nextLine();
System.out.print("Content: ");
String content = scanner.nextLine();
for (int i = 0; i < questions.size(); i++) {
Question question = questions.get(i);
if (question.getSubject().equals(subject) && question.getContent().equals(content)) {
questions.remove(i);
saveQuestionsToFile();
System.out.println("Question deleted.");
return;
}
}
System.out.println("Question not found.");
}
}
```
Main类:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
QuestionManager questionManager = new QuestionManager();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter your choice:");
System.out.println("1. List all questions");
System.out.println("2. Search by subject");
System.out.println("3. Add question");
System.out.println("4. Delete question");
System.out.println("5. Quit");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
questionManager.listAllQuestions();
break;
case 2:
System.out.print("Subject: ");
String subject = scanner.nextLine();
questionManager.searchBySubject(subject);
break;
case 3:
questionManager.addQuestion();
break;
case 4:
questionManager.deleteQuestion();
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice.");
}
}
}
}
```
以上代码实现了一个简单的控制台题库管理系统,包括列出所有试题信息、按科目查询、添加试题、删除试题、退出系统的功能。您可以根据需求进行修改和完善。注意,在本示例中,题目信息以逗号分隔保存在文件中,您也可以选择其他方式进行存储。
阅读全文