public static void main(String[] args){
Student stu;
Scanner scan;
//定义一个List集合用于只存放Student对象
List<Student> list = new ArrayList<Student>();
//通过for循环表示要输入5个学生的信息
for(int i=1;i<=5;i++){
//总成绩
int total=0;
//每循环一次就创建一个Student对象
stu = new Student();
System.out.println("请输入第"+i+"个学生的姓名及成绩:");
System.out.print("姓名:");
//通过for循环来分别获得一个Student对象的4个成员信息
for(int j=0;j<4;j++){
//创建一个Scanner对象来获取键盘录入的值
scan =new Scanner(System.in);
//通过Scanner类中的nextLine()方法获得此次键盘录入的值
String field =scan.nextLine();
//将键盘录入的值对号入座
if(j==0){
stu.setName("姓名:"+field);
System.out.print("语文:");
}
if(j==1){
//将成绩加入到总成绩中
total += Integer.valueOf(field);
stu.setCscore("语文:"+field);
System.out.print("数学:");
}
if(j==2){
total += Integer.valueOf(field);
stu.setMscore("数学:"+field);
System.out.print("英语:");
}
if(j==3){
total += Integer.valueOf(field);
stu.setEscore("英语:"+field);
}
}
public void setName(String name) {
this.name = name;
}
public int getLangScore() {
return langScore;
}
public void setLangScore(int langScore) {
this.langScore = langScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnlishScore() {
return EnlishScore;
}
public void setEnlishScore(int enlishScore) {
EnlishScore = enlishScore;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
@Override
public int compareTo(Student stu) {//重写方法
int i= new Integer(this.sum).compareTo(
new Integer(stu.sum));
if(i==0){
return this.name.compareTo(stu.name);
}
return i ;
}
public static void main(String[] args) throws IOException {
Comparator<Student> com = Collections.reverseOrder(); //自定义一个比较器
File f = new File("d:/stu.txt");
Set<Student> se = StudnetIn(com);//强比较器传入以及排序,返回集合
ToFile(se,f);
}
public static void ToFile(Set<Student> se, File f) throws IOException {
Iterator<Student> it = se.iterator();
FileWriter fw = new FileWriter(f);
while(it.hasNext()){
Student s = it.next();
fw.write(s.toString()+s.getSum()+"\t"+"\r\n");
}
fw.close();
}
public static Set<Student> StudnetIn(Comparator<Student> com) {
Scanner s = new Scanner(System.in);
Set<Student> se = null;
se = new TreeSet<Student>(com);//构造TreeSet的时候就选择了