java打分系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.ioTest;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
 
public class Exem_Scode2 {
 
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        File f1 = new File("F:\\exam02");// 创建操作目录的文件对象
        System.out.println("有多少人参见考试?");
        int personSum = input.nextInt(); // 从控制台获取本次考试的人数
        System.out.println("有多少题?");
        int tiSum = input.nextInt(); // 从控制台获取本次考试的题数
 
        /*
         *
         * 获得.txt文件所在目录的路径的字符串表达形式的数组
         *
         *
         */
        String[] fPtah = f1.list();
         
        int a = 0;
        String[] path = new String[personSum];// 考试者提供的答案文件数组// 获得的.txt文件数组
        for (String str : fPtah) {
            System.out.println(str);
            if (str.endsWith(".txt")) {
                path[a] = "E:\\\\exam02\\\\" + str;
                a++;
            }
        }
 
        try {
            TestScore(personSum, tiSum, path);// 调用功能方法(实现自动打分,并统计正确率)
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void TestScore(int personSum, int tiSum, String[] fPtah) throws IOException {
 
        BufferedReader br = null;// 读人数
        BufferedReader br1 = null;// 读满分(正确答案,比对对象)
        BufferedWriter bw = null;// 写结果
        try {
            int sum = 0; // 记录每个人的总分
            int jige = 0;// 统计及格的人数
            int[] f = new int[tiSum];// 记录每题正确的题数
            bw = new BufferedWriter(new FileWriter("e:\\exam02\\score.bak", true));// 写结果
 
            for (int i = 0; i < fPtah.length; i++) {// 遍历每一个考试者的答案
                // System.out.println();
                br = new BufferedReader(new FileReader(fPtah[i]));// 读人数(考试者的答案),按行读取
                System.out.println("---------------");
                br1 = new BufferedReader(new FileReader("e:\\exam02\\满分.bak"));// 读满分(正确答案,比对对象),按行读取
                // System.out.println(files[i].getPath());
                for (int j = 0; j < tiSum; j++) {// 遍历每一行的答案
                    if (br.readLine().trim().equals(br1.readLine().trim())) {// 比对每一行的答案判断是否相等
                        sum += 5;// 累加每一个人的分数
                        f[j]++;// 统计每题做对的人数
                    }
                    System.out.println("sum" + sum);// 测试语句
                }
                StringBuffer bs=new StringBuffer();
                bs.append((fPtah[i] + "  "+"成绩:" + sum + "分").replace("E:\\\\exam02\\\\", " ").replace(".txt", "")
                        .trim());
                System.out.println(bs.toString());// 测试语句
                if (sum >= 60) {
                    jige++;// 统计及格的人数
                }
                bw.write(bs.toString());// 把数据写入文件(答案日志)
                bw.newLine();
                sum = 0;// 把总分置空,等待下次累加
            }
            StringBuffer bs1=new StringBuffer();
            bs1.append("总人数" + fPtah.length + "\t" + "及格人数" + jige);
            bw.write(bs1.toString());// 把数据写入文件(答案日志)
            bw.newLine();
 
            /*
             * 统计正确率 遍历每一题,统计每一题做对的人数,然后计算正确率输出
             *
             *
             */
            StringBuffer bs2=null;;
            for (int k = 0; k < tiSum; k++) {
                bs2=new StringBuffer();
                System.out.println(f[k]);
                int t1 = (int) (f[k] / 20.0 * 100);// 计算正确率
                bs2.append("题" + (k + 1) + "正确率为" + t1 + "%");
//              String zql = "题" + (k + 1) + "正确率为" + t1 + "%";// 记录每一题的正确率
                bw.write(bs2.toString());// 把数据写入文件(答案日志)
                bw.newLine();// 换行
            }
            bw.flush();// 刷新缓冲区
 
        } finally {
            if (br1 != null) {
                br1.close();
            }
            if (br != null) {
                br.close();
            }
            if (bw != null) {
                bw.close();
            }
        }
    }
}

  

posted @   浪涛飞  阅读(1483)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示