- 键盘录入学生的语文,数学,英语成绩。按照总分排序从高到下写入文本中.*/
- public class Test8 {
-
- public static void main(String[] args) throws IOException {
-
- lur();
- }
- public static void lur() throws IOException{
- InputStreamReader or = new InputStreamReader(System.in);
- BufferedReader br = new BufferedReader(or);
- TreeMap<student,Integer> hm = new TreeMap<student,Integer>();
- OutputStreamWriter ow = new
- OutputStreamWriter(new FileOutputStream("d://1.txt"));
- BufferedWriter bw = new BufferedWriter(ow);
- String st = null;
- while((st=br.readLine())!=null){
- if("over".equals(st)){
- break;
- }
- else{
- String[] str = st.split(",");
- student su = new student(str[0],Integer.parseInt(str[1]),Integer.parseInt(str[2]),
- Integer.parseInt(str[3]));
- hm.put(su,su.getsum());
- }
- }
- List<student> li = (List<student>)hm.keySet();
- Collections.sort(li);
- Collections.reverse(li);
- for(student s : li){
- String sn = new String(s+"/t"+hm.get(s));
- bw.write(sn);
- bw.newLine();
- }
- br.close();
- bw.close();
- }
- }
- class student implements Comparable<student> {
- private String name;
- private int math;
- private int ch;
- private int english;
- private int sum;
- student(String name,int math,int ch,int english){
- this.name=name;
- this.math=math;
- this.ch=ch;
- this.english=english;
- }
- public int getsum(){
- return math+ch+english;
- }
- public int compareTo(student s){
- int num = new Integer(this.sum).compareTo(s.sum);
- if(num==0){
- return this.name.compareTo(s.name);
- }
- else{
- return num;
- }
- }
- public String toString(){
- return name+","+math+","+ch+","+english;
- }
- }
-
复制代码 程序没报错,为什么运行不了啊?
|