1:已知文件student.txt中内容是学生的姓名和各科的成绩
// 张三:81.5,86.0,93.5
// 李四:78.0,84.0,90.0
// 王五:59.0,75.5,81.5
// (Student.txt文件和内容可手动创建);
// 2:读取文件内容,计算出总分和平均分;在项目根目录下创建一个“成绩明细.txt”,将计算出的内容(以平均分从小到大)
// 写入“成绩明细.txt”文件中,格式是:
// 王五:59.0,75.5,81.5 总分:216.0 平均分:72.0
// 李四:78.0,84.0,90.0 总分:252.0 平均分:84.0
// 张三:81.5,86.0,93.5 总分:261.0 平均分:87.0
// 注意:成绩明细.txt文件里面顺序;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("student.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("成绩明细.txt"));
ArrayList<String> list = new ArrayList<String>();
String st = null;
while ((st = br.readLine()) != null) {
list.add(st);
}
HashMap<Double, String> map = new HashMap<>();
for (String string : list) {
double sum = 0;
String[] split = string.split(":");
String cj = split[1];
String[] split2 = cj.split(",");
sum += Double.valueOf(split2[0])+Double.valueOf(split2[1])+Double.valueOf(split2[2]);
double su = sum / split2.length;
StringBuffer sb = new StringBuffer();
sb.append(string+" 总分: " + sum+" 平均分:" + su);
map.put(su, sb.toString());
}
double[] dou = new double[3];
Set<Double> keySet = map.keySet();
int do1 = 0;
for (Double double1 : keySet) {
dou[do1] = double1;
do1++;
}
Arrays.sort(dou);
for (double e : dou) {
StringBuffer sb = new StringBuffer();
sb.append(map.get(e));
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
} |
|