package judgescore;
import java.io.IOException;
import java.util.Random;
public class Student {
private String name;
Student(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [name=" + name + "]";
}
Answer ans = new Answer();
void answer2question(int m, String path) {
Random random = new Random();
for (int i = 0; i < m; i++)
ans.writeAnswer(i, new Integer(random.nextInt(5) + 97).toString());
// 创建学生名的文件
try {
ans.writeOut(path + "\\" + name+".txt");
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
package judgescore;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
public class Answer {
HashMap<Integer, String> hm = new HashMap<Integer, String>();
void writeAnswer(int i, String answer) {
hm.put(i, answer);
}
void writeOut(String path) throws IOException {
FileWriter fw = new FileWriter(path);
String s = hm.toString();
fw.write(s.substring(1, s.length() - 1).replace(",", "\r\n"));
fw.close();
}
}
package judgescore;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Random;
public class RightAnswer extends Answer {
ArrayList<String> list = new ArrayList<String>();
void writeOut(String path, int m) throws IOException {
Properties pro = new Properties();
for (int i = 0; i < m; i++)
pro.setProperty(new Integer(i).toString(), list.get(i));
FileWriter fw = new FileWriter(path + "\\" + "answer.properties");
pro.store(fw, "---");
fw.close();
}
void setAnswer(int m,String path) {
Random random = new Random();
for (int i = 0; i < m; i++)
list.add(i, new Integer(random.nextInt(5) + 97).toString());
try {
writeOut(path,m);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
package judgescore;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Random;
public class ScoreOne extends RightAnswer{
void setScore(String path, int m) throws IOException {
Properties pro = new Properties();
for (int i = 0; i < m; i++)
pro.setProperty(new Integer(i).toString(), new Integer(new Random().nextInt(30)).toString());
FileWriter fw = new FileWriter(path + "\\" + "score.properties");
pro.store(fw, "---");
fw.close();
}
}
package judgescore;
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.Properties;
import java.util.TreeMap;
public class Count {
static String path = "E:\\test";
static int m = 100;
public static void main(String[] args) {
// TODO 自动生成的方法存根
new Student("张飞").answer2question(m, path);
new Student("关羽").answer2question(m, path);
new Student("刘备").answer2question(m, path);
new Student("孙权").answer2question(m, path);
new Student("曹操").answer2question(m, path);
new Student("诸葛亮").answer2question(m, path);
new Student("袁绍").answer2question(m, path);
new Student("吕布").answer2question(m, path);
new RightAnswer().setAnswer(m, path);
// 把每道题的分数奥文件中
try {
new ScoreOne().setScore(path, m);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// 读取每个人的文件,然后一一与正确答案比较,并统计出正确数量,按照名字——分数统计出来写出文件
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
try {
gaizuoye(map);
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path + "\\" + "score.txt")));
bw.write(map.toString());
bw.close();
} catch (NumberFormatException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
private static void gaizuoye(TreeMap map) throws NumberFormatException, IOException {
// 获取正确答案和分数
Properties pro1 = new Properties();
BufferedReader answer = new BufferedReader(new FileReader(path + "\\" + "answer.properties"));
pro1.load(answer);
BufferedReader score = new BufferedReader(new FileReader(path + "\\" + "score.properties"));
Properties pro2 = new Properties();
pro2.load(score);
// 遍历学生成绩文件
File file = new File(path);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
if (!files[i].getName().contains(".properties")&&!files[i].getName().contains("score.txt")) {
BufferedReader br = new BufferedReader(new FileReader(files[i]));
String s = null;
int j = 0;
int sum = 0;
while ((s = br.readLine()) != null) {
if (s.substring(s.lastIndexOf("=") + 1)
.equals(pro1.getProperty(new Integer(j).toString()).toString())) {
sum += Integer.parseInt(pro2.getProperty(new Integer(j).toString()));
System.out.println(s.substring(s.lastIndexOf("=") + 1) + "=="
+ pro1.getProperty(new Integer(j).toString()));
j++;
}
map.put(files[i].getName().substring(0, files[i].getName().lastIndexOf(".")), sum);
}
System.out.println();
br.close();
}
}
}
写的比较散,貌似有点长了。 |