基于Java控制台的在线考试系统

在线考试系统

这是一个基于控制台的考试系统, 可以开启考试,

可以查看上次考试成绩.

需求说明

考试系统要实现:

菜单打印, 考试, 查看上次考试成绩, 退出系统等功能.

项目步骤

(1) 通过Idea创建一个名为ExaminationSystem的包, 在包ExaminationSystem里创建一个考题类ExamItem, 包含考试题目title, 选项一optionA, 选项二optionB, 选项三optionC, 选项四optionD, 正确答案answer属 性.

(2) 在包ExaminationSystem里创建一个考试服务类ExamService, 包含一个考题列表itemList(List< ExamItem >类型), 答案数组answerArr (String[]类型), 得分score.包含一个初始化方法public void ui(), 在ui()方法中写程序的流程, 完成每个菜单的功能. 包含一个初始化方法public void init(), 开始考试方法public void startExam(), 判卷的方法private void judgeExam(), 保存用户答案和成绩的方法public void save(), 打印上次考试成绩的方法public void printLastExam(). init方法的作用是从 Items.txt 文件中读取数据, 把数据封装成ExamItem对象, 把对象存入 itemList中, 并根据itemList的size大小, 初始化answerArr数组. 在ExamService的无参构造方法里 调用init方法进行初始化.  startExam的作用是开始考试, 打印考题, 处理用户操作. judgeExam的作用是根据用户的答案, 和正确答案进行判卷. save的作用是保存用户答案和成绩, 把自己每道题的答案, 考试得分存入 result.txt 文件中. printLastExam的作用是打印上次考试的成绩, 即save中存储的内容.

(3) 在ExaminationSystem包里创建一个考试系统类main,  在main方法中启动ui()界面方法.

 ExaminationSystem包,和类的总览:

 一:在包ExaminationSystem里先创建创建一个考题类ExamItem

包含考试题目title, 选项一optionA, 选项二optionB, 选项三optionC, 选项四optionD, 正确答案answer属 性.

代码:

package ExaminationSystem;

public class ExamItem {
    private String title;
    private String optionA;
    private String optionB;
    private String optionC;
    private String optionD;
    private String answer;

    public ExamItem() {
    }

    public ExamItem(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;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getOptionA() {
        return optionA;
    }

    public void setOptionA(String optionA) {
        this.optionA = optionA;
    }

    public String getOptionB() {
        return optionB;
    }

    public void setOptionB(String optionB) {
        this.optionB = optionB;
    }

    public String getOptionC() {
        return optionC;
    }

    public void setOptionC(String optionC) {
        this.optionC = optionC;
    }

    public String getOptionD() {
        return optionD;
    }

    public void setOptionD(String optionD) {
        this.optionD = optionD;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

}

二:在包ExaminationSystem里创建一个考试服务类ExamService

在ExamService类中实现各个功能,总代码在最下面

在ExamService类全局中定义一个名为itemList的List集合用于存放题库;定义一个名为answerArr的字符串数组,用于存放自己的答案

各个功能的实现代码和效果图:
(1) 打印菜单ui()方法

打印考试系统的所有功能菜单. 程序一启动就需要打印菜单, 等待用户选择. 用户执行完某一菜单功能之 后, 需要重新打印菜单, 等待用户继续操作. 例如: 查看上次成绩功能完成之后, 需要重新打印菜单.

 效果图

代码:

 public void ui(){
        while (true) {
            System.out.println("================");
            System.out.println("1.进入考试");
            System.out.println("2.查看上次考试成绩");
            System.out.println("3.退出");
            System.out.println("请选择:");
            int str = sc.nextInt();
            switch (str) {
                case 1:
                    startExam();
                    break;
                case 2:
                    printLastExam();
                    break;
                case 3:
                    System.out.println("你确定要退出系统吗(Y/N)");
                    String s = sc.next();
                    if (s.equalsIgnoreCase("Y")) {
                        System.out.println("退出成功!");
                        System.exit(0);
                    } else {
                        System.out.println("取消退出!");
                    }
                default:
                    System.out.println("你输入的信息有误,请重新输入!");
                    break;
            }
        }

    }
(2) 读取考试题库文件Items.txt

进入考试前,先把考试题库从Items.txt文件中读取出来,放入List集合中,方便后续操作

(3) 进入考试startExam()方法

当用户选择菜单1的时候, 进入考试功能. 考试功能一进来先提示欢迎信息(即操作说明).

(1) 操作说明

 (2) 正式考试

用户输入"N" 正式进入考试, 先打印第一道题的题目和选项, 等待用户操作. 如果用户输入的不是"N", 无法开始考试, 让用户继续输入, 直到输入"N". 无论输入"N"还是"n", 都可以进入考试.

 (3) 答题

输入完自己的答案之后(无论正确与否), 会自动进入下一题 功能按键输入大写小写都要能识别.

(4)按N进入下一题

按N或n进入下一题

如果是最后一题,那么按N或n还是最后一题

 (5)按P返回上一题

按P或p返回上一题,如果是第一题返回的还是第一题

(6)按N或P如果题已经做过了就提示此题已做,并显示做过的答案

 (7)按F交卷

按F或f调用judgeExam()方法提前交卷,或答完题目后自动调用judgeExam()方法,并计算得了多少分(每题的分数是=100/总题数)

 

 (8)如果按其他键,则提示输入错误,请重新输入

 交卷judgeExam()代码:

  public void judgeExam(){

            for (int i=0;i<itemList.size();i++){
                if (answerArr[i]!=null){ //不判断是否为空,会报空指针异常
                    if (answerArr[i].equalsIgnoreCase(itemList.get(i).getAnswer())){
                        score+=100/itemList.size();
                    }
                }


            }


        System.out.println("您的答案是:"+ Arrays.toString(answerArr)+",您的得分是:"+score);
        save(); //调用save()将自己写的答案和分数保存到指定文件里
    }

 startExam()方法代码:

  public void startExam(){
        answerArr=new String[itemList.size()];
        System.out.println("-----------欢迎进入考试-----------");
        System.out.println();
        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();
        System.out.println("请按N键进入考试。。。");
        String s = sc.next();
        if (itemList!=null) {
                for (int i = 0; i < itemList.size(); i++) {

                    if (s.equalsIgnoreCase("N")) {

                        ExamItem examItem = itemList.get(i);
                        System.out.println(examItem.getTitle());
                        System.out.println(examItem.getOptionA());
                        System.out.println(examItem.getOptionB());
                        System.out.println(examItem.getOptionC());
                        System.out.println(examItem.getOptionD());
                        if (answerArr[i]!=null){
                            System.out.println("您这道题已经作答。选的是" + answerArr[i] + "。输入A-D更换答案,P上一题,N下一题,F交卷:");
                        }
                        System.out.println("请输入您的选择:");
                        String answer = sc.next();
                        if (answer.equalsIgnoreCase("a")||answer.equalsIgnoreCase("b")||answer.equalsIgnoreCase("c")||answer.equalsIgnoreCase("d")){
                            answerArr[i] = answer;
                        } else if (answer.equalsIgnoreCase("n")) {

                            if (i == itemList.size()-1) {
                                i--;
                            }

                        }else if (answer.equalsIgnoreCase("p")) {

                            if (i == 0) {
                                i--;
                            } else {
                                i-=2;
                            }

                        } else if (answer.equalsIgnoreCase("F")) {
                            i = itemList.size();


                        }else {
                            i--;
                            System.out.println("您输入的信息有误,请重新输入!");
                        }


                    }


                }//for循环的括号
            } //不为空的括号
        judgeExam();
//        }

    }
 (4)保存答案save()方法

save方法是把自己选择的答案保存到result.txt文件中,若result.txt文件不存在,调用save方法会自动建一个名为result.txt文件.

save代码:

 public void save(){
        File file=new File("src/ExaminationSystem/result.txt");
        LocalDateTime now = LocalDateTime.now();
        try {
            BufferedWriter bf=new BufferedWriter(new FileWriter(file));
            bf.write(Arrays.toString(answerArr)+"你的分数为:"+score+"\t作答时间:"+now);
            bf.close();
            score=0;  //在进入考试时,分数从零分考试.
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
 (5) 查看上次考试成绩printLastExam()方法

用户选择菜单2的时候, 查看上次考试成绩, 如果有题目没有做, 答案显示null,并显示作答时间

当关闭当前系统时,下次再运行时,也可以查看上次成绩

 

 printLastExam()方法代码:

 public void printLastExam(){
        File file=new File("src/ExaminationSystem/result.txt");
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String ss=null;
            while ((ss=br.readLine())!=null){ //一行一行的读,这样就不会出现只读半个汉字的情况而出现乱码
                System.out.println(ss);
            }
            br.close();
            if (file.length()==0){
                System.out.println("您还没有作答!");
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
(6)退出系统

按3退出程序,并提示是否退出

ExamService类总代码:

package ExaminationSystem;

import lanouzaixiankaoshixitong.ExamItem;

import java.io.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class ExamService {
    private List<lanouzaixiankaoshixitong.ExamItem> itemList=null;
    private String[] answerArr=null;
    private int score;
    private Scanner sc=new Scanner(System.in);

    public void init(){
        List<String> list=new ArrayList();
        try {
            BufferedReader br=new BufferedReader(new FileReader("src/ExaminationSystem/Items.txt"));
            String s=null;
            while ((s=br.readLine())!=null){
                if (s.trim().length()>0){
                    list.add(s);
                }
            }

            itemList=new ArrayList<>();

                for (int i = 0; i < list.size(); i += 6) {
                    String title = list.get(i);
                    String optionA = list.get(i + 1);
                    String optionB = list.get(i + 2);
                    String optionC = list.get(i + 3);
                    String optionD = list.get(i + 4);
                    String answer = list.get(i + 5);
                    lanouzaixiankaoshixitong.ExamItem examItem = new lanouzaixiankaoshixitong.ExamItem(title, optionA, optionB, optionC, optionD, answer);
                    itemList.add(examItem);

                }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void ui(){
        while (true) {
            System.out.println("================");
            System.out.println("1.进入考试");
            System.out.println("2.查看上次考试成绩");
            System.out.println("3.退出");
            System.out.println("请选择:");
            int str = sc.nextInt();
            switch (str) {
                case 1:
                    startExam();
                    break;
                case 2:
                    printLastExam();
                    break;
                case 3:
                    System.out.println("你确定要退出系统吗(Y/N)");
                    String s = sc.next();
                    if (s.equalsIgnoreCase("Y")) {
                        System.out.println("退出成功!");
                        System.exit(0);
                    } else {
                        System.out.println("取消退出!");
                    }
                default:
                    System.out.println("你输入的信息有误,请重新输入!");
                    break;
            }
        }

    }

    public void startExam(){
        answerArr=new String[itemList.size()];
        System.out.println("-----------欢迎进入考试-----------");
        System.out.println();
        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();
        System.out.println("请按N键进入考试。。。");
        String s = sc.next();
        if (itemList!=null) {
                for (int i = 0; i < itemList.size(); i++) {

                    if (s.equalsIgnoreCase("N")) {

                        ExamItem examItem = itemList.get(i);
                        System.out.println(examItem.getTitle());
                        System.out.println(examItem.getOptionA());
                        System.out.println(examItem.getOptionB());
                        System.out.println(examItem.getOptionC());
                        System.out.println(examItem.getOptionD());
                        if (answerArr[i]!=null){
                            System.out.println("您这道题已经作答。选的是" + answerArr[i] + "。输入A-D更换答案,P上一题,N下一题,F交卷:");
                        }
                        System.out.println("请输入您的选择:");
                        String answer = sc.next();
                        if (answer.equalsIgnoreCase("a")||answer.equalsIgnoreCase("b")||answer.equalsIgnoreCase("c")||answer.equalsIgnoreCase("d")){
                            answerArr[i] = answer;
                        } else if (answer.equalsIgnoreCase("n")) {

                            if (i == itemList.size()-1) {
                                i--;
                            }

                        }else if (answer.equalsIgnoreCase("p")) {

                            if (i == 0) {
                                i--;
                            } else {
                                i-=2;
                            }

                        } else if (answer.equalsIgnoreCase("F")) {
                            i = itemList.size();


                        }else {
                            i--;
                            System.out.println("您输入的信息有误,请重新输入!");
                        }


                    }


                }//for循环的括号
            } //不为空的括号
        judgeExam();
//        }

    }
    //分隔符
    public void judgeExam(){

            for (int i=0;i<itemList.size();i++){
                if (answerArr[i]!=null){ //不判断是否为空,会报空指针异常
                    if (answerArr[i].equalsIgnoreCase(itemList.get(i).getAnswer())){
                        score+=100/itemList.size();
                    }
                }


            }


        System.out.println("您的答案是:"+ Arrays.toString(answerArr)+",您的得分是:"+score);
        save(); //调用save()将自己写的答案和分数保存到指定文件里
    }
    public void save(){
        File file=new File("src/ExaminationSystem/result.txt");
        LocalDateTime now = LocalDateTime.now();
        try {
            BufferedWriter bf=new BufferedWriter(new FileWriter(file));
            bf.write(Arrays.toString(answerArr)+"你的分数为:"+score+"\t作答时间:"+now);
            bf.close();
            score=0;  //在进入考试时,分数从零分考试.
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    public void printLastExam(){
        File file=new File("src/ExaminationSystem/result.txt");
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String ss=null;
            while ((ss=br.readLine())!=null){ //一行一行的读,这样就不会出现只读半个汉字的情况而出现乱码
                System.out.println(ss);
            }
            br.close();
            if (file.length()==0){
                System.out.println("您还没有作答!");
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public ExamService() {
        init();
    }
}

三:在包ExaminationSystem里创建一个mian类(启动程序的类)

在main类中的main方法里创建ExamService类对象,调用ExamService类里面的ui方法

package ExaminationSystem;
public class main {
    public static void main(String[] args) {
        ExamService e=new ExamService();
        e.ui();
    }
}

 总结:

本程序总共三个类考题类ExamItem类,考试服务类ExamService类,运行程序main类,三个程序的总代码都在上面.还有一个考试题库文件Items.txt,自动生成的存放答案的result.txt文件.

  • 37
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不太会敲代码的搬砖工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值