1.Examination考试题目
该代码定义了一个名为Examination的Java类,用于表示考试题目。类中包含题目标题、四个选项和正确答案的属性,以及对应的构造方法和getter/setter方法。具体功能如下:
- 提供无参构造方法和全参构造方法,用于创建Examination对象。
- 提供getter方法,用于获取题目标题、选项和答案。
- 提供setter方法,用于设置题目标题、选项和答案。
java
package com.hualan.bean;
public class Examination {
private String title;
private String optionA;
private String optionB;
private String optionC;
private String optionD;
private String answer;
public Examination() {
}
public Examination(String title, String optionA, String optionB, String optionC, String optionD, String answer) {
this.title = title;
this.optionA = optionA;
this.optionB = optionB;
this.optionC = optionC;
this.optionD = optionD;
this.answer = answer;
}
/**
* 获取
* @return title
*/
public String getTitle() {
return title;
}
/**
* 设置
* @param title
*/
public void setTitle(String title) {
this.title = title;
}
/**
* 获取
* @return optionA
*/
public String getOptionA() {
return optionA;
}
/**
* 设置
* @param optionA
*/
public void setOptionA(String optionA) {
this.optionA = optionA;
}
/**
* 获取
* @return optionB
*/
public String getOptionB() {
return optionB;
}
/**
* 设置
* @param optionB
*/
public void setOptionB(String optionB) {
this.optionB = optionB;
}
/**
* 获取
* @return optionC
*/
public String getOptionC() {
return optionC;
}
/**
* 设置
* @param optionC
*/
public void setOptionC(String optionC) {
this.optionC = optionC;
}
/**
* 获取
* @return optionD
*/
public String getOptionD() {
return optionD;
}
/**
* 设置
* @param optionD
*/
public void setOptionD(String optionD) {
this.optionD = optionD;
}
/**
* 获取
* @return answer
*/
public String getAnswer() {
return answer;
}
/**
* 设置
* @param answer
*/
public void setAnswer(String answer) {
this.answer = answer;
}
}
2.Examination系统业务层
这段代码实现了一个简单的考试系统,主要功能如下:
- 初始化试题:从exam.txt文件中读取题目、选项和答案,并存储到examList中。
- 显示考试说明:通过showExam方法向用户展示考试规则。
- 开始考试:通过startExam方法让用户答题,支持选择答案、切换题目和交卷功能。
- 判分:通过judgeExam方法对比用户答案与正确答案,计算总分。
- 保存结果:将用户的答案和分数保存到result.txt文件中。
- 查看上次考试结果:通过printLastExam方法读取并打印上次考试的结果。
java
package com.hualan.service;
import com.hualan.bean.Examination;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ExamSystem {
private List<Examination> examList = new ArrayList<>();
private String[] answerArr ;
private int score;
public ExamSystem() {
init();
}
public void init() {
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("exam.txt"), "UTF-8"))) {
String line;
while ((line = br.readLine()) != null) {
// 跳过空行
if (line.trim().isEmpty()) {
continue;
}
String title = line;
String optionA = br.readLine();
while (optionA != null && optionA.trim().isEmpty()) {
optionA = br.readLine();
}
String optionB = br.readLine();
while (optionB != null && optionB.trim().isEmpty()) {
optionB = br.readLine();
}
String optionC = br.readLine();
while (optionC != null && optionC.trim().isEmpty()) {
optionC = br.readLine();
}
String optionD = br.readLine();
while (optionD != null && optionD.trim().isEmpty()) {
optionD = br.readLine();
}
String answer = br.readLine();
while (answer != null && answer.trim().isEmpty()) {
answer = br.readLine();
}
if (title != null && optionA != null && optionB != null && optionC != null && optionD != null && answer != null) {
Examination examina = new Examination(title, optionA, optionB, optionC, optionD, answer);
examList.add(examina);
}
}
answerArr = new String[examList.size()];
} catch (IOException e) {
e.printStackTrace();
}
}
public void showExam(){
System.out.println("------欢迎进入考试------");
System.out.println("使用以下按键进行考试:");
System.out.println("A - D:选择指定答案");
System.out.println("P:显示上一题");
System.out.println("N:显示下一题");
System.out.println("F:交卷");
System.out.println("请按N键进入考试...");
}
public void startExam(){
Scanner sc = new Scanner(System.in);
int index = 0;
showExam();
String input = sc.nextLine();
while (!input.equals("N")){
System.out.println("无法开始考试,请重新输入N键进入考试");
input = sc.nextLine();
}
while (true){
Examination examina = examList.get(index);
System.out.println("第" + (index + 1) + "题 " + examina.getTitle());
System.out.println( examina.getOptionA());
System.out.println( examina.getOptionB());
System.out.println( examina.getOptionC());
System.out.println( examina.getOptionD());
if (answerArr[index]!=null){
System.out.println("您已经选择过答案了,请选择其他按键");
}
input = sc.nextLine();
if (input.equalsIgnoreCase("A") || input.equalsIgnoreCase("B") || input.equalsIgnoreCase("C") || input.equalsIgnoreCase("D")) {
answerArr[index] = input.toUpperCase();
if (index < examList.size() - 1) {
index++;
} else {
System.out.println("这是最后一题,已自动交卷!");
judgeExam();
save();
return;
}
} else if (input.equalsIgnoreCase("N")) {
if (index < examList.size() - 1) {
index++;
} else {
System.out.println("这是最后一题,已自动交卷!");
judgeExam();
save();
return;
}
} else if (input.equalsIgnoreCase("P")) {
if (index > 0) {
index--;
} else {
System.out.println("这是第一题,没有上一题!");
}
} else if (input.equalsIgnoreCase("F")) {
judgeExam();
save();
return;
} else {
System.out.println("无效输入,请重新输入!");
}
}
}
private void judgeExam() {
score = 0;
for (int i = 0; i < examList.size(); i++) {
if (answerArr[i] != null && answerArr[i].equals(examList.get(i).getAnswer())) {
score++;
}
}
score = score * 10;
}
public void save() {
try (PrintWriter pw = new PrintWriter(new FileWriter("result.txt"))) {
StringBuilder answerStr = new StringBuilder();
for (String answer : answerArr) {
answerStr.append(answer).append(",");
}
if (answerStr.length() > 0) {
answerStr.setLength(answerStr.length() - 1);
}
pw.println(answerStr + ",上次考试分数是:" + score);
} catch (IOException e) {
e.printStackTrace();
}
}
public void printLastExam() {
File file = new File("result.txt");
if (!file.exists()) {
System.out.println("尚未参加考试,请先考试!");
return;
}
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line = br.readLine();
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.ExamSystem系统实现层
该代码实现了一个简单的在线考试系统,功能包括:
- 打印菜单供用户选择。
- 用户可选择进入考试、查看上次考试成绩或退出系统。
- 根据用户输入调用相应方法处理逻辑,若输入无效则提示重新输入。
java
package com.hualan.main;
import com.hualan.service.ExamSystem;
import java.util.Scanner;
class ExamMain {
public static void printMenu() {
System.out.println("华蓝在线考试系统");
System.out.println("1.进入考试");
System.out.println("2.查看上次考试成绩");
System.out.println("3.退出系统");
System.out.println("请选择:");
}
public static void main(String[] args) {
ExamSystem examService = new ExamSystem();
Scanner scanner = new Scanner(System.in);
while (true) {
printMenu();
String choice = scanner.nextLine();
switch (choice) {
case "1":
examService.startExam();
break;
case "2":
examService.printLastExam();
break;
case "3":
System.out.println("退出考试系统");
System.exit(0);
default:
System.out.println("无效选择,请重新输入!");
}
}
}
}