//创建set方法便于修改成员属性
public void setName()
{
this.name = name;
}
public void setAge()
{
this.age = age;
}
public void setScore()
{
this.score = score;
}
//创建获取属性的get方法
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getScore()
{
return score;
}
}
//自定义比较器,主要是按照成绩由高到低排序
class Mycompare implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
//先判断成绩,按成绩由高到低排序
if (s2.getScore() > s1.getScore())
return 1;
public static void chucun(TreeSet<Student> ts)throws IOException{
File f = new File("Stud.txt");
if( !f.exists()) f.createNewFile();
BufferedWriter bs = new BufferedWriter(new FileWriter(f));
/*BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream("Stud.txt"));
Iterator<Student> it = ts.iterator();*/
Iterator<Student>it = ts.iterator();
Student s = null;
while(it.hasNext()){
s = it.next();
bs.write(s.getName());
bs.write(s.getMath()+"");
bs.write(s.getEnglish()+"");
bs.write(s.getChina()+"");
bs.newLine();
bs.flush();
}
bs.close();
}
public static Student createStudent()throws IOException{
BufferedReader bs = null;
String s = null;
int m = 0;
int e = 0;
int c = 0;
Student st= null;
try{
bs = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入学生姓名");
s = bs.readLine();
System.out.println("请输入学生数学成绩");
m = Integer.parseInt(bs.readLine());
System.out.println("请输入学生英语成绩");
e = Integer.parseInt(bs.readLine());
System.out.println("请输入学生语文成绩");
c = Integer.parseInt(bs.readLine());
st = new Student(s,m,c,e);
System.out.println(st.getName()+"::"+st.getMath()+"::"+st.getEnglish()+"::"+st.getChina()+"::"+st.jisuan());
//return st;
}catch(IOException er){
System.out.println(er.toString());
}
return st;
}
}
class Student implements Comparable<Student>{
private String name;
private int m, e, c;
int sum = 0;
Student(String name, int m, int c, int e){
this.name = name;
this.m = m;
this.c = c;
this.e = e;
}
public int compareTo(Student s1){
return -(this.jisuan() - s1.jisuan());
}
public void setName(String name){
this.name = name;
}
public void setMath(int m){
this.m = m;
}
public void setEnglish(int e){
this.e = e;
}
public void setChina(int c){
this.c = c;
}
public String getName(){
return name;
}
public int getMath(){
return m;
}
public int getEnglish(){
return e;
}
public int getChina(){
return c;
}
public int jisuan(){
sum = m+e+c;
return sum;
}
}作者: 殇_心。 时间: 2013-5-12 10:47 如果问题已解决,请及时修改分类,否则继续提问,谢谢合作!