I/O流之分析成绩单
来源:互联网 发布:pubwin2015免刷卡软件 编辑:程序博客网 时间:2024/06/05 13:21
姓名:张三,数学72分,物理67分,英语70分.姓名:李四,数学92分,物理98分,英语88分.姓名:周五,数学68分,物理80分,英语77分.
把以上成绩单按行读入,输出总成绩,平均分,最高分,写入到socreAnalysis.txt文件中
import java.io.*;import java.util.*;public class AnalysisResult { public static void main(String args[]) { File fRead = new File("score.txt"); File fWrite = new File("socreAnalysis.txt"); try { Writer out = new FileWriter(fWrite, true);//以尾加方式创建指向文件fWrite的out流 BufferedWriter bufferWrite = new BufferedWriter(out);//创建指向out的bufferWrite流 Reader in = new FileReader(fRead);//创建指向文件fRead的in流 BufferedReader bufferRead = new BufferedReader(in); //创建指向in的bufferRead流 String str = null; while ((str = bufferRead.readLine()) != null) { double totalScore = Fenxi.getTotalScore(str); str = str + "总分:" + totalScore; double i=totalScore; double averageScore = Fenxi.getAverageScore(str); str = str + "平均分:" + averageScore; double totalestScore = Fenxi.getTotalestScore(str,i); str = str + "最高分:" + totalestScore; System.out.println(str); } bufferRead.close(); bufferWrite.close(); } catch (IOException e) { System.out.println(e.toString()); } }}import java.util.*;public class Fenxi { public static double getAverageScore(String s) { Scanner scanner = new Scanner(s); scanner.useDelimiter("[^0123456789.]+"); double averageScore = 0; while (scanner.hasNext()) { try { double totalScore = scanner.nextDouble(); averageScore =totalScore / 3; } catch (InputMismatchException exp) { String t = scanner.next(); } } return averageScore; } public static double getTotalScore(String s) { Scanner scanner = new Scanner(s); scanner.useDelimiter("[^0123456789.]+"); double totalScore = 0; while (scanner.hasNext()) { try { double score = scanner.nextDouble(); totalScore = totalScore + score; } catch (InputMismatchException exp) { String t = scanner.next();bufferWrite.write(str); bufferWrite.newLine();}}
return totalScore;}public static double getTotalestScore(String s,double i) {Scanner scanner = new Scanner(s);scanner.useDelimiter("[^0123456789.]+");double totalestScore = 0;while (scanner.hasNext()) {try {double Score = scanner.nextDouble();if(Score<i){if (Score > totalestScore) {
totalestScore = Score;
}
else {}
}
else{}
}catch (InputMismatchException exp) {
String t = scanner.next();
}
}
return totalestScore;
}
}
1绿色部分为了限制总分被读进去,影响最高分。不能把i换为100,有错,比如30,30,30,最高分=总分90是错误的
2黄色部分没写输出没有数据3正确运行结果如图
0 0
- I/O流之分析成绩单
- Java I/O API之性能分析
- FFmpeg架构之I/O模块分析
- FFmpeg架构之I/O模块分析 .
- FFmpeg架构之I/O模块分析
- FFmpeg架构之I/O模块分析
- FFmpeg架构之I/O模块分析
- FFmpeg架构之I/O模块分析
- I/O多路复用之select函数分析
- I/O多路复用之poll函数分析
- FFmpeg架构之I/O模块分析
- FFmpeg架构之I/O模块分析
- C++之I/O流
- Java之I/O流
- Java之I/O流
- I/O之字节流
- 关于I/O流的类分析
- Java I/O流概念分析整理
- 自动旋转的太极图
- Unity学习
- Intellig IDEA创建web项目(非maven)
- 这是我的第一个博客,好紧张
- 顺序查找法查找元素在数组中所在位置
- I/O流之分析成绩单
- css reset重置样式有那么重要吗?
- 用户自动登录案例分析
- LINK - 服务器间网络通讯错误
- maven 将依赖包打入war中
- Virtual Memory(虚拟内存)
- 笔记本Ubuntu14.04双显卡安装cuda7.5步骤
- 字符数组作业
- UI布局two---对话框