码迷,mamicode.com
首页 > 编程语言 > 详细

java打分系统

时间:2017-02-26 01:06:35      阅读:732      评论:0      收藏:0      [点我收藏+]

标签:with   测试   core   final   res   path   多少   print   路径   

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();
			}
		}
	}
}

  

java打分系统

标签:with   测试   core   final   res   path   多少   print   路径   

原文地址:http://www.cnblogs.com/java-le/p/6443439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!