🔺Iteam类
package domain;
public class Item {
private String question;
private String[] options;
private char answer;
public Item(String question, String[] options, char answer) {
this.question = question;
this.options = options;
this.answer = answer;
}
public Item() {
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String[] getOptions() {
return options;
}
public void setOptions(String[] options) {
this.options = options;
}
public char getAnswer() {
return answer;
}
public void setAnswer(char answer) {
this.answer = answer;
}
@Override
public String toString() {
return question + "\n" +
options[0] + "\n" +
options[1] + "\n" +
options[2] + "\n" +
options[3] + "\n标准答案:" + answer + "\n";
}
}
🔺IteamService类
package service;
import domain.Item;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ItemService {
private Item[] items;
//txt在项目外的code文件中,默认从根目录开始查找,需要修改运行配置
private final String ITEM_FILENAME = "./Items.txt";
private final String ANSWER_FILENAME = "./answer.dat";
private final int LINES_PER_ITEM = 6;
public final int TOTAL_ITEMS;
public ItemService(){
List<String> list = readTextFile(ITEM_FILENAME);
TOTAL_ITEMS = list.size() / LINES_PER_ITEM;
items = new Item[TOTAL_ITEMS];
for (int i = 0; i < TOTAL_ITEMS; i++) {
String question = list.get(i * LINES_PER_ITEM);
String[] options = {
list.get(i * LINES_PER_ITEM + 1),
list.get(i * LINES_PER_ITEM + 2),
list.get(i * LINES_PER_ITEM + 3),
list.get(i * LINES_PER_ITEM + 4)
};
char answer = list.get(i * LINES_PER_ITEM + 5).charAt(0);
items[i] = new Item(question,options,answer);
}
}
private List<String> readTextFile(String filename){
FileReader fr = null;
BufferedReader br = null;
List<String> content = new ArrayList<>();
try{
fr = new FileReader(filename);
br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null){
if(!line.trim().equals(""))content.add(line);
}
}catch (FileNotFoundException e){
System.out.println(e.getMessage());
}catch (IOException e){
e.printStackTrace();
}finally {
if(br != null){
try {
br.close();
}catch (IOException e){
System.out.println(e.getMessage());
}
}
if(fr != null){
try {
fr.close();
}catch (IOException e){
System.out.println(e.getMessage());
}
}
}
return content;
}
public Item getItem(int no){
if(no <= 0 || no > TOTAL_ITEMS){
return null;
}
return items[no -1];
}
public void saveAnswer(char[] answer){
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(ANSWER_FILENAME);
oos = new ObjectOutputStream(fos);
oos.writeObject(answer);
}catch (IOException e){
System.out.println(e.getMessage());
}finally {
if(oos != null){
try {
oos.close();
}catch (IOException e){
System.out.println(e.getMessage());
}
}
}
}
public char[] readAnswer(){
char[] answer = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(ANSWER_FILENAME);
ois = new ObjectInputStream(fis);
answer = (char[]) ois.readObject();
}catch (Exception e){
System.out.println(e.getMessage());
}finally {
if(ois != null){
try {
ois.close();
}catch (IOException e){
System.out.println(e.getMessage());
}
}
}
return answer;
}
}
🔺Exam类
package test;
import view.ExamView;
public class Exam {
public static void main(String[] args) {
ExamView examView = new ExamView();
examView.enterMainMenu();
}
}
🔺ExamView
package view;
import domain.Item;
import service.ItemService;
import java.util.Scanner;
public class ExamView {
private ItemService itemService = new ItemService();
private char[] answer;
public ExamView(){
answer = new char[itemService.TOTAL_ITEMS];
for (int i = 0; i < answer.length; i++) {
answer[i] = ' ';
}
}
public void enterMainMenu(){
while (true){
displayMainMenu();
char key = getUserAction();
switch (key){
case '1' -> testExam();
case '2' -> reviewLastExam();
case '3' -> {
if(confirmEnd("请确认是否退出(Y/N)"))
return;
}
}
}
}
public char getUserAction(){
char[] validKey = {'1','2','3','A','B','C','D','E','F','N','P','Y'};
char key = 0;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String str = scanner.next();
if(str.length() != 1)
continue;
str = str.toUpperCase();
key = str.charAt(0);
for(char k: validKey){
if(k == key){
return key;
}
}
}
return key;
}
public void displayItem(int no){
if(no < 1 || no > itemService.TOTAL_ITEMS)
return;
Item item = itemService.getItem(no);
System.out.println();
System.out.println();
System.out.println(item.getQuestion());
String[] options = item.getOptions();
for(String option : options){
System.out.println(option);
}
System.out.println();
if(answer[no - 1] != ' '){
System.out.println("您已选择的答案:" + answer[no - 1]);
}
}
public void testExam(){
int curItem = 1;
displayWelcomInfo();
while (true) {
displayItem(curItem);
System.out.println("请选择正确答案(p-上一题 n-下一题)");
char key = getUserAction();
switch (key) {
case 'A':
case 'B':
case 'C':
case 'D':
answer[curItem - 1] = key;
case 'N':
if (curItem < itemService.TOTAL_ITEMS) {
++curItem;
} else {
System.out.println("已到达最后一题");
}
break;
case 'P':
if (curItem > 1) {
--curItem;
} else {
System.out.println("已到达第一题");
}
case 'F':
if(confirmEnd("确认是否结束考试(Y/N)")){
itemService.saveAnswer(answer);
displayResults(answer);
return;
}
break;
default:
System.out.println("输入无效");
}
}
}
private boolean confirmEnd(String msg){
System.out.println();
System.out.println(msg);
while (true){
char key = getUserAction();
if(key != 'N' && key != 'Y')
continue;
return (key == 'Y');
}
}
private void displayWelcomInfo(){
System.out.println();
System.out.println();
System.out.println("-----------欢迎进入考试-----------");
System.out.println();
System.out.println(" 使用以下按键进行考试:");
System.out.println();
System.out.println(" A-B:选择指定答案");
System.out.println(" P :显示上一题");
System.out.println(" N :显示下一题");
System.out.println(" F :结束考试");
System.out.println();
System.out.print(" 请按N键进入考试...");
while (true){
char key = getUserAction();
if(key == 'N')
break;
}
}
private void displayResults(char[] charAnswer){
int score = 0;
for (int i = 0; i < itemService.TOTAL_ITEMS; i++) {
Item item = itemService.getItem(i + 1);
if(charAnswer[i] == item.getAnswer()){
score += 10;
}
}
System.out.println("序 号 标准答案 你的答案");
for (int i = 0; i < itemService.TOTAL_ITEMS; i++) {
Item item = itemService.getItem(i + 1);
System.out.println("第" + ((i < 9) ? " " : "") + (i + 1)
+ " 题 " + item.getAnswer() + " "
+ charAnswer[i]);
}
System.out.println("恭喜,本次考试成绩为:" + score + " 分");
}
private void displayMainMenu(){
System.out.println();
System.out.println();
System.out.println("-------欢迎使用在线考试系统-------");
System.out.println();
System.out.println(" 1 进入考试");
System.out.println(" 2 查看成绩");
System.out.println(" 3 系统退出");
System.out.println();
System.out.print(" 请选择...");
}
private void reviewLastExam(){
System.out.println();
System.out.println();
char[] charAnswer = itemService.readAnswer();
displayResults(charAnswer);
}
}