javaSE考试系统

 创建一个考试服务类,完成各个功能

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

public class ExamService {
    static List<ExamItem> listItem = new ArrayList<>();
    static String[] answers = new String[10];
    static String[] myanswers = new String[10];
    private static int score ;
    private static int index;
    public static void init(){
        List<String> list = new ArrayList<>();
        String str = null;
        try (FileReader fileReader = new FileReader("C:\\Users\\25221\\Desktop\\xuexi\\Items.txt");
             BufferedReader bufferedReader = new BufferedReader(fileReader);){
            while ((str = bufferedReader.readLine())!=null){
                list.add(str);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int a= 0;
        for (int i = 0;i<list.size();i+=7){
            String title = list.get(i);
            String aObject = list.get(i+1);
            String bObject = list.get(i+2);
            String cObject = list.get(i+3);
            String dObject = list.get(i+4);
            String answer = list.get(i+5);
            ExamItem examItem = new ExamItem(title,aObject,bObject,cObject,dObject);
            listItem.add(examItem);
            answers[a]= answer;
           a++;
        }
    }
    public  static  void showHelp(){

    }
    public  static void startExam() {
        for (int i = 0; i < myanswers.length; i++) {
           myanswers[i] = null;
        }
        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("         请按N键进入考试!");
        Scanner scanner = new Scanner(System.in);
         index = 0;
        boolean end = false;
        while (end == false) {
            System.out.print("请输入你的选择(P:上一题 N:下一题 F:结束考试):");
            String str = scanner.nextLine();
            String s = str.toUpperCase();
            if (s.equals("P")||s.equals("N")||s.equals("F")) {
                switch (s) {
                    case "P":
                        index--;
                        if (index == -1) {
                            System.out.println("请输入N进入第一题");
                            index = 0;
                        } else if (index == 0) {
                            System.out.println("这是第一题,没有上一题。");
                            index++;
                        } else {
                            index--;
                            System.out.println(listItem.get(index));
                            if (myanswers[index] == null) {
                               inYourAnswer();
                                index++;
                            } else {
                                System.out.println("本题你的答案是:" + myanswers[index] + "  " + "是否要修改答案?(Y/N)");
                                Scanner scannerchange = new Scanner(System.in);
                                String change = scannerchange.nextLine();
                                String changeup = change.toUpperCase();
                                if (changeup.equals("Y")) {
                                    inYourAnswer();
                                }
                                index++;
                            }
                        }
                        break;
                    case "N":
                        if (index == 10) {
                            System.out.println("这是最后一题,可以输入F结束考试");
                            index--;
                        } else {
                            System.out.println(listItem.get(index));
                            if (myanswers[index] == null) {
                                inYourAnswer();
                            } else {
                                System.out.println("本题你的答案是:" + myanswers[index] + "  " + "是否要修改答案?(Y/N)");
                                Scanner scannerchange = new Scanner(System.in);
                                String change = scannerchange.nextLine();
                                String changeup = change.toUpperCase();
                                if (changeup.equals("Y")) {
                                    inYourAnswer();
                                }
                            }
                        }
                        index++;
                        break;
                    case "F":
                        System.out.print("是否确定结束考试?请输入(Y/N):");
                        Scanner scanner1 = new Scanner(System.in);
                        String s1 = scanner1.nextLine();
                        String s1up = s1.toUpperCase();
                        if (s1up.equals("Y")) {
                            end = true;
                            System.out.println("您已提交考试");
                            printAnswer();
                            System.out.println("您的考试成绩是:" + judgeExam());
                            save();
                        } else if (s1up.equals("N")) {
                            System.out.println("请输入P或N");
                        } else {
                            System.out.println("请输入正确的选项");
                        }
                        break;
                }
            }else {
                System.out.println("请重新输入!");
            }
        }
    }
    private static int judgeExam(){
        score = 0;
        for (int i = 0;i<answers.length;i++){
            if (answers[i].equals(myanswers[i])){
                score+=10;
            }
        }
        return score;
    }
    public static  void printAnswer(){
            System.out.print(Arrays.toString(myanswers));
    }
    public  static void save(){

        try(FileOutputStream fout = new FileOutputStream("C:\\Users\\25221\\Desktop\\xuexi\\score.txt",true);
            Writer w = new OutputStreamWriter(fout, "GBK");) {
            PrintWriter pw = new PrintWriter(w);
            int myscore= judgeExam();
            pw.println(myscore);
            pw.close();
        } catch (FileNotFoundException e) {
           e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void printLastExam(){
        try( FileReader fr = new FileReader("C:\\Users\\25221\\Desktop\\xuexi\\score.txt");
             BufferedReader br = new BufferedReader(fr);) {
            String line;
            String lastLine = null;
            while ((line = br.readLine())!=null){
                    lastLine = line;
            }
            if (lastLine!=null){
                System.out.println("上次成绩为:"+lastLine);
            }else {
                System.out.println("上次成绩为空");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void inYourAnswer(){
        System.out.print("输入你的答案:");
        Scanner scannerp = new Scanner(System.in);
        String myanswer = scannerp.nextLine();
        String myanswerup = myanswer.toUpperCase();
        boolean b = false;
        while (b==false){
            if (myanswerup.equals("A")||myanswerup.equals("B")||myanswerup.equals("C")||myanswerup.equals("D")){
                myanswers[index] = myanswerup;b=true;}else {
                System.out.print("请重新输入你的选项:");
                Scanner newscannerp = new Scanner(System.in);
                String newmyanswer = newscannerp.nextLine();
                myanswerup = newmyanswer.toUpperCase();
            }
        }
    }
}

创建一个考试题目的类用来记录考试题目

public class ExamItem {
    private String title;
    private String aOption;
    private String bOption;
    private String cOption;
    private String dOption;
    private String answer;

    public String getTitle() {
        return title;
    }

    public ExamItem(String title, String aOption, String bOption, String cOption, String dOption) {
        this.title = title;
        this.aOption = aOption;
        this.bOption = bOption;
        this.cOption = cOption;
        this.dOption = dOption;
    }
    @Override
    public String toString() {
        return title + "\n"+aOption+"\n"+bOption+"\n"+cOption+"\n"+dOption+"\n";
    }
}

创建测试类,对考试系统进行测试,在控制台输出测试结果

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        boolean leave = false;
        while (leave == false){
            menu();
            int choice = scanner.nextInt();
            switch (choice){
                case 1:
                    ExamService.init();
                    ExamService.startExam();
                    break;
                case 2:
                    ExamService.printLastExam();
                    break;
                case 3:
                    leave = true;
                    System.out.println("您已退出考试程序");
                    break;
            }
        }
    }
    public static void menu(){
        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("请选择:");
    }
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值