黑马程序员技术交流社区
标题:
21天,IO流练习题 StudentInfoTest
[打印本页]
作者:
noiary
时间:
2014-11-20 21:51
标题:
21天,IO流练习题 StudentInfoTest
本帖最后由 noiary 于 2014-11-21 13:33 编辑
整个晚上都在写这个,还没看比老师的答案,勉强写出来,
目测没多少参考价值。小伙伴们见笑啦{:3_50:}
总分没有进行排序,熬不动了,碎觉!
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/*
有五个学生,每个学生有3门课的成绩,
从键盘输入以上数据(包括姓名,三门课成绩),
输入的格式:如:zhagnsan,30,40,60计算出总成绩,
并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。
思路:
1.创建学生对象Student
2.建立键盘录入流 ,对录入数据进行缓存
3.创建文件写入流
*/
public class StudentInfoTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = null;
int count = 0;
while((line = bufr.readLine()) != null) {
//结束语句 over
if(line.equals("over"))
break;
//检测输入合适是否正确
String[] strArr = parseLine(line);
if(strArr.length == 0)
continue;
/*写入文件*/
writeFile(strArr);
bufw.write(line);
bufw.newLine();
bufw.flush();
if(++count == 5) {
System.out.println("输入完毕,程序结束。");
break;
}
}
bufr.close();
bufw.close();
}
@SuppressWarnings("unused")
private static String[] parseLine(String line) {
String[] falshArr = new String[0];
String[] strArray = line.split(",");
if(strArray.length == 1) {
strArray = line.split(",");
if (strArray.length == 1)
System.out.println("请输入正确格式:如zhagnsan,30,40,60");
return falshArr;
}
if(strArray.length == 4 && strArray[0] instanceof String) {
for(int i=1; i<4; i++) {
try {
Integer.parseInt(strArray[i]);
}
catch (NumberFormatException e) {
System.out.println("请输入正确成绩:如zhagnsan,30,40,60");
return falshArr;
}
}
return strArray;
}
return falshArr;
}
public static void writeFile(String[] strArr) throws IOException {
String name = strArr[0];
int classResult1 = Integer.parseInt(strArr[1]);
int classResult2 = Integer.parseInt(strArr[2]);
int classResult3 = Integer.parseInt(strArr[3]);
Student stu = new Student(name, classResult1, classResult2, classResult3);
BufferedWriter bufw = new BufferedWriter(new FileWriter("stud.txt",true));
bufw.write(stu.getName() + " :\t" + stu.getTotalResult());
bufw.newLine();
bufw.close();
}
}
/*创建学生对象*/
class Student {
private String name;
private int classResult1;
private int classResult2;
private int classResult3;
Student(String name, int cr1, int cr2, int cr3) {
this.name = name;
this.classResult1 = cr1;
this.classResult2 = cr2;
this.classResult3 = cr3;
}
/*获取总成绩*/
public int getTotalResult() {
return this.classResult1 + this.classResult2 + this.classResult3;
}
/*获取姓名*/
public String getName() {
return this.name;
}
}
复制代码
作者:
noiary
时间:
2014-11-21 16:00
看完视频觉得自己写得是一坨屎 - -。
//package day21_IOStream;
/*
有五个学生,每个学生有3门课的成绩,
从键盘输入以上数据(包括姓名,三门课成绩),
输入的格式:如:zhagnsan,30,40,60计算出总成绩,
并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。
思路:
1.创建学生对象Student
2.建立键盘录入流 ,对录入数据进行缓存
3.创建文件写入流
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.TreeSet;
public class StudentInfoTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Set<Student> stus = StudentInfoTool.getStudents();
StudentInfoTool.writeToFile(stus);
}
}
/* 学生工具类 */
class StudentInfoTool {
public static Set<Student> getStudents() throws IOException {
Set<Student> stuSet = new TreeSet<Student>();
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
while ((line = bufr.readLine()) != null) {
if (line.equals("over"))
break;
String[] strs = parseLine(line);
/* 格式不正确 */
if (strs == null) {
System.out.println("请输入正确成绩:如zhagnsan,30,40,60");
continue;
}
Student stu = new Student(strs[0], Integer.parseInt(strs[1]),
Integer.parseInt(strs[2]), Integer.parseInt(strs[2]));
stuSet.add(stu);
}
return stuSet;
}
public static void writeToFile(Set<Student> stus) throws IOException {
File file = new File("stu.txt");
BufferedWriter bufw = new BufferedWriter(new FileWriter(file, true));
for (Student s : stus) {
bufw.write(s + "\t");
bufw.write(s.getSum() + "");
bufw.newLine();
bufw.flush();
}
bufw.close();
}
private static String[] parseLine(String line) {
String[] strArray = line.split(",");
if (strArray.length == 1) {
strArray = line.split(",");
if (strArray.length == 1)
return null;
}
if (strArray.length == 4 && strArray[0] instanceof String) {
for (int i = 1; i < 4; i++) {
try {
Integer.parseInt(strArray[i]);
} catch (NumberFormatException e) {
return null;
}
}
return strArray;
}
return null;
}
}
/* 创建学生对象 */
class Student implements Comparable<Student> {
private String name;
/* 三门成绩及总分 */
private int cr1, cr2, cr3, sum;
Student(String name, int cr1, int cr2, int cr3) {
this.name = name;
this.cr1 = cr1;
this.cr2 = cr2;
this.cr3 = cr3;
sum = cr1 + cr2 + cr3;
}
/* 排序 */
public int compareTo(Student s) {
int num = new Integer(s.sum).compareTo(sum);
if (num == 0)
return name.compareTo(s.name);
return num;
}
/* 获取总成绩 */
public int getSum() {
return sum;
}
/* 获取姓名 */
public String getName() {
return this.name;
}
public boolean equals(Object obj) {
if (!(obj instanceof Student))
return false;
Student stu = (Student) obj;
return name.equals(stu.name) && cr1 == stu.cr1 && cr2 == stu.cr2
&& cr3 == stu.cr3;
}
public int hashCode() {
return name.hashCode() + sum * 42;
}
public String toString() {
return "student[" + name + ", " + cr1 + ", " + cr2 + ", " + cr3 + "]";
}
}
复制代码
作者:
scoto263
时间:
2014-11-21 20:33
一坨屎也是屎壳郎的宝贝。有价值。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2