public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Set<Student> set = new TreeSet<Student>();
for(;;) {
String line = in.readLine();
if("qult".equals(line)) {
in = new BufferedReader(new FileReader("student.txt"));
continue;
}else if(line == null) {
in.close();
break;
}
String[]arr = line.split(",");
Student stu = new Student(arr[0] + Integer.parseInt(arr[1])+ Integer.parseInt(arr[2])+ Integer.parseInt(arr[3]));
set.add(stu);
}
PrintStream out = new PrintStream(new FileOutputStream("student.txt"));
System.setOut(out);
for(Student s : set)
System.out.println(s);
out.close();
}
}
public class Student implements Comparator<Student> {
private String name;
private int math;
private int chinese;
private int english;
private int num;
public Student(String name) {
super();
this.name = name;
this.math = math;
this.chinese = chinese;
this.english = english;
num = math + chinese + english;
}
public String toString() {
return name + "," + math + ","+ chinese + "," + english;
}
public int compare(Student o1, Student o2) {
int numGap = o1.math - o2.math;
int nameGap = o1.name.compareTo(o2.name);
return numGap != 0 ? numGap : nameGap;
}
}
try {
br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
Set<Students> set = new TreeSet<Students>();
while ((line = br.readLine()) != null) {
if ("over".equals(line))
break;
String[] stuinfo = line.split(",");
Students s = new Students(stuinfo[0],
Integer.parseInt(stuinfo[1]),
Integer.parseInt(stuinfo[2]),
Integer.parseInt(stuinfo[3]));
set.add(s);
}
PrintWriter pw = new PrintWriter(System.out,true);//定义一个打印流,将输出打印到控制台
for(Students s : set){
pw.println(s.toString());
//pw.println(s.getSum());
}
/*
* 定义一个学生类,实现Comparable接口,使其就有比较性
* 要比较,就要实现equals方法和hashcode方法。
*/
class Students implements Comparable<Students> {
private String name;
private int chinese;
private int math;
private int english;
private int sum;
public Students(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
this.sum = chinese + math + english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChanese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public int hashCode() {
return this.name.hashCode() + this.sum % 2*7;
}
public boolean equals(Object obj) {
if (obj instanceof Students)
throw new RuntimeException("类型匹配");
Students stu = (Students) obj;
return this.name.equals(stu.name);
}
public String toString() {
return name + "[" + chinese + "," + math + "," + english + "]"
+ " " + sum;
}
/*
* 复写比较方法,如果学生的总成绩相等,则按学生姓名的自然循序排序
*/
public int compareTo(Students o) {
int num = new Integer(this.sum).compareTo(new Integer(o.sum));
if (num == 0)
return this.name.compareTo(o.name);
return num;
}
}给一个我刚写的程序。你可以对照的看下作者: 秦超 时间: 2012-5-14 22:28
陈忠 发表于 2012-5-14 21:44
看了一下你的程序,出现问题的地方很多,对于集合的内容好像还没有掌握。import java.io.BufferedReader;
i ...