import java.io.*;
import java.util.*;
class StudText
{
public static void main(String[] args)throws IOException
{
Set<StudentCard> scc = StuTool.getStudent();
StuTool.creFile(scc);
}
}
//创建的学生对象
class Student implements Comparable<Student>
{
private String name;
private int math , yuwen , english; //三科成绩
private int sum = 0;
StudentCard(String name , int math , int lang , int forlang)
{
this.name = name;
this.math = math;
this.lang = lang;
this.forlang = forlang;
sum = math + lang + forlang; //总分
}
public int hashCode()
{
return name.hashCode() + sum*20;
}
public boolean equals(Object obj)
{
if(!(obj instanceof StudentCard))
throw new RuntimeException("类型不匹配");
StudentCard sc = (StudentCard)obj;
return this.name.equals(name) && this.sum == sum;
}
public int compareTo(StudentCard sc)
{
int num = new Integer(this.sum).compareTo(new Integer(sum));
if(num == 0)
return this.name.compareTo(name);
return num;
}
public int getSum()
{
return sum;
}
public String toString()
{
return "姓名:" +name + " 成绩:" +math + "," +yuwsen +"," + english;
}
}
class StuTool
{
public static Set<Student> getStudent() throws IOException
{
BufferedReader bur = new BufferedReader(new InputStreamReader(System.in));
Set<Student> stud = new TreeSet<Student>();
String s = null;
while((s = bur.readLine()) != null)
{
if("over".equals(s))
break;
String[] arr = s.split(",");
Student stu = new StudentCard(arr[0] , Integer.parseInt(arr[1]) ,
Integer.parseInt(arr[2]) ,
Integer.parseInt(arr[3]));
stud.add(stu);
}
bur.close();
return stud;
}
public static void creFile(Set<Student> stud) throws IOException
{
BufferedWriter buw = new BufferedWriter(new FileWriter("stud.txt"));
for(Student sc : stud)
{
buw.write(sc.toString() + "\t");
buw.write(sc.getSum() + "");
buw.newLine();
buw.flush();
}
buw.close();
}
}
跟视频对了好几遍,为什么生成的文件只有一行数据? 哪里有问题 郁闷
|