public class 学生成绩 {
/**
.学生类包含四个属性:名字,语数外英的成绩。要求键盘录入五个学生的信息,
按照他们的总分从大到小排序,之后写入到d:\number.txt文件中。
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
TreeSet<student>t=new TreeSet<student>(new Comparator<student>() {
@Override
public int compare(student s1, student s2) {
int x=s2.getSum()-s1.getSum();
return x==0?1:x;
}
});
while(t.size()<3){
System.out.println("请输入学生成绩格式如下:姓名,语文,数学,英语");
String s=sc.nextLine();
String s1=",";
String[]s2=s.split(s1);
int yvwen=Integer.parseInt(s2[1]);
int math=Integer.parseInt(s2[2]);
int english=Integer.parseInt(s2[3]);
t.add(new student(s2[0],yvwen,math,english));
}
BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\number.txt"));
for (student st : t) {
bw.append(st.toString());
bw.newLine();
}
bw.close();
}
}
class student{
private String name;
private int yvwen;
private int math;
private int english;
private int sum;
public student() {
super();
}
public student(String name, int yvwen, int math, int english) {
super();
this.name = name;
this.yvwen = yvwen;
this.math = math;
this.english = english;
this.sum=yvwen+math+english;
}
@Override
public String toString() {
return "student [name=" + name + ", yvwen=" + yvwen + ", math=" + math
+ ", english=" + english + ", sum=" + sum + "]";
}
public int getSum(){
return sum;
}
} |