写一个小程序,从文件中读取试题,显示给考生,并记录考生的答案。然后和答案比对。答题过程中提示考生答题时间。最后输出考生的成绩和试题数。

public class Examv2 {
    //定义两个属性。score为分数,list用来存考生存入的答案
    private int score;
    private ArrayList<String> list;
    //get()、set()方法
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public ArrayList<String> getList() {
        return list;
    }
    public void setList(ArrayList<String> list) {
        this.list = list;
    }
    //内部类,用来计时。提示学生答题时间
    class TimeThread extends TimerTask {
        int i = 6;
        @Override
        public void run() {
            if (i > 0) {
                System.out.println("时间剩余:" + (--i) + "分");
            } else {
                System.out.println("时间到");
                try {
                    Examv2.GiveScore(Examv2.this);                 
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) throws Exception {
        //定义一个对象
        Examv2 e = new Examv2();
        //定义Timer
        Timer timer = new Timer();
        timer.schedule(e.new TimeThread(), 0, 1000*60);
        //定义相关成员变量
        int inqestionnum = 0;
        int index = 0;
        e.setScore(0);
        e.setList(new ArrayList<String>());
        String answer = null;
        //定义scanner,用于考生写入答案
        Scanner scanner = new Scanner(System.in);
        //从题库抽取试题,并显示给考生。同时将考生答案写入list
        File file = new File("question/question.txt");
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        while (true) {
            String str = br.readLine();
            index++;
            if (index == 6) {
                index = 1;
                inqestionnum++;
                System.out.print("你选则的答案:");
                answer = scanner.next();
                e.getList().add(answer);
            }
            if (str == null) {
                break;
            }
            System.out.println(str);
        }
        System.out.println("题目总数为:" + inqestionnum);
        // 调用判分函数。判定得分
        GiveScore(e);
    }
    //判分函数,同时推出程序
    static void GiveScore(Examv2 e) throws FileNotFoundException,
            UnsupportedEncodingException, IOException {
        File file1 = new File("question/answer.txt");
        FileInputStream fis1 = new FileInputStream(file1);
        InputStreamReader isr1 = new InputStreamReader(fis1, "UTF-8");
        BufferedReader br1 = new BufferedReader(isr1);
        String stranswer = br1.readLine();
        System.out.println("正确答案为:" + stranswer);
        String[] strArr = stranswer.split("\\|");
        for (int i = 0; i < e.getList().size(); i++) {
            String string = e.getList().get(i);
            if (string.equals(strArr[i])) {
                e.setScore(e.getScore() + 1);
            }
        }
        System.out.println("你的答案为:" + e.getList());
        System.out.println("得分为:" + e.getScore());
        System.exit(0);
    }
}

运行结果:

时间剩余:5分

1、question one()

A、one

B、two

C、three

D、four

你选则的答案:a

2、question two()

A、one

B、two

C、three

D、four

你选则的答案:b

3、question three()

A、one

B、two

C、three

D、four

你选则的答案:c

4、question four()

A、one

B、two

C、three

D、four

你选则的答案:a

5、question five()

A、one

B、two

C、three

D、four

你选则的答案:a


题目总数为:5

正确答案为:a|b|c|d|a

你的答案为:[a, b, c, a, a]

得分为:4

如果考生没有做完所有题。并且答题时间结束,则直接输出考生的成绩并结束程序

时间剩余:5分

1、question one()

A、one

B、two

C、three

D、four

你选则的答案:a

2、question two()

A、one

B、two

C、three

D、four

你选则的答案:c

3、question three()

A、one

B、two

C、three

D、four

你选则的答案:时间剩余:4分

时间剩余:3分

时间剩余:2分

时间剩余:1分

时间剩余:0分

时间到

正确答案为:a|b|c|d|a

你的答案为:[a, c]

得分为:1