package com.itheima;
import java.io.*;
import java.util.*;
/**
* 7,键盘录入学生的语文,数学,英语成绩。按照总分排序从高到下写入文本中
* xxx 80 80 80
*/
public class Test7 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new FileWriter(new File("C:\\Users\\16F4\\Desktop\\Test\\1.txt")));
String len = null;
TreeSet<students> ts = new TreeSet<>();
while((len = bufr.readLine())!= null){
if("end".equals(len)){
break;
}else {
String[] str = len.split(" +");
String name = str[0];
int Chinese = 0;
int Math = 0 ;
int EngLish = 0;
Chinese = toInt(str[1]);
Math = toInt(str[2]);
EngLish = toInt(str[3]);
ts.add(new students(name, Chinese, Math, EngLish));
}
}
Iterator<students> it = ts.iterator();
while(it.hasNext()){
students s = it.next();
bufw.write(s.name + " " +s.Chinese+ " " +s.Math+ " " +s.EngLish);
bufw.newLine();
}
bufr.close();
bufw.close();
}
public static int toInt(String string) {
// TODO Auto-generated method stub
int x = 0;
char[] c = string.toCharArray();
//System.out.println(c.length);
for (int i = 0; i < c.length; i++) {
int y = c[i] - '0';
for (int j = 0; j < c.length - i - 1; j++) {
y *= 10;
}
x += y;
}
//System.out.println(x);
return x;
}
}
class students implements Comparable<Object>{
String name;
int Chinese;
int Math;
int EngLish;
public students(String name,int Chinese,int Math,int EngLish) {
// TODO Auto-generated constructor stub
this.name = name;
this.Chinese = Chinese;
this.Math = Math;
this.EngLish = EngLish;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return Chinese;
}
public void setChinese(int chinese) {
Chinese = chinese;
}
public int getMath() {
return Math;
}
public void setMath(int math) {
Math = math;
}
public int getEngLish() {
return EngLish;
}
public void setEngLish(int engLish) {
EngLish = engLish;
}
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
if(!(obj instanceof students))
throw new RuntimeException();
students s = (students)obj;
if((this.Math+this.Chinese+this.EngLish)>(s.Chinese+ s.EngLish + s.Math))
return 1;
return -1;
}
}
|