import java.io.*;
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int ma,cn,en;
private int sum;
Student(String name, int ma, int cn, int en)
{
this.name = name;
this.ma = ma;
this.cn = cn;
this.en = en;
sum = ma+cn+en;
}
public String getName()
{
return this.name;
}
public int getSum()
{
return sum;
}
public int hashCode()
{
return this.name.hashCode()+sum*78;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student)obj;
return this.name.equals(s) && this.sum==s.sum;
}
public int compareTo(Student s)
{
int num = new Integer(this.sum).compareTo(new Integer(s.sum));
if(num==0)
return this.name.compareTo(s.name);
return num;
}
public String toString()
{
return "student["+name+","+ma+","+cn+","+en+"]";
}
}
class StuInfoTool
{
public static Set<Student> getStudents()throws IOException
{
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
Set<Student> s = new TreeSet<Student>();
String line = null;
while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
String[] str = line.split(",");
Student stu = new Student(str[0],new Integer(str[1]),new Integer(str[2]),new Integer(str[3]));
s.add(stu);
}
bufr.close();
return s;
}
public static void write2File(Set<Student> s)throws IOException
{
BufferedWriter bufw = new BufferedWriter(new FileWriter("stutest.txt"));
for(Student str : s)
{
bufw.write(str.toString()+"\t");
bufw.write(str.getSum()+"");
bufw.newLine();
bufw.flush();
}
bufw.close();
}
}
class StuInfoTest
{
public static void main(String[] args) throws IOException
{
Set<Student> s = StuInfoTool.getStudents();
StuInfoTool.write2File(s);
}
}
为什么结果是这样的,第一个和最后一个,怎么没有对齐呢,试了几次都是这样
student[lisi,32,34,56] 122
student[zhangsan,33,44,55] 132
student[zhouqi,34,56,78] 168
student[wangwu,35,67,86] 188
student[zhaoliu,32,67,89] 188
student[sunba,45,67,89] 201
|