A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 天蝎降降温 中级黑马   /  2014-10-20 12:20  /  690 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. 键盘录入学生的语文,数学,英语成绩。按照总分排序从高到下写入文本中.*/
  2. public class Test8 {

  3.        
  4.         public static void main(String[] args) throws IOException {
  5.                
  6.                   lur();
  7.         }
  8.         public static  void lur() throws IOException{
  9.                    InputStreamReader or = new InputStreamReader(System.in);
  10.                BufferedReader br = new BufferedReader(or);
  11.                TreeMap<student,Integer> hm = new TreeMap<student,Integer>();
  12.            OutputStreamWriter ow = new
  13.            OutputStreamWriter(new FileOutputStream("d://1.txt"));
  14.            BufferedWriter bw = new BufferedWriter(ow);
  15.            String st = null;
  16.            while((st=br.readLine())!=null){
  17.                    if("over".equals(st)){
  18.                            break;
  19.                    }
  20.                    else{
  21.                    String[] str = st.split(",");
  22.                    student su  =  new student(str[0],Integer.parseInt(str[1]),Integer.parseInt(str[2]),
  23.                                    Integer.parseInt(str[3]));
  24.                    hm.put(su,su.getsum());                  
  25.                    }
  26.                    }
  27.                    List<student> li = (List<student>)hm.keySet();
  28.                    Collections.sort(li);
  29.                    Collections.reverse(li);
  30.                    for(student s : li){
  31.                            String sn = new String(s+"/t"+hm.get(s));
  32.                            bw.write(sn);
  33.                            bw.newLine();                         
  34.                    }
  35.            br.close();
  36.            bw.close();
  37.           }
  38. }
  39. class student implements Comparable<student> {
  40.           private String name;
  41.           private int math;
  42.           private int ch;
  43.           private int english;
  44.           private int sum;
  45.           student(String name,int math,int ch,int english){
  46.                     this.name=name;
  47.                     this.math=math;
  48.                     this.ch=ch;
  49.                     this.english=english;
  50.           }
  51.           public int getsum(){
  52.                    return math+ch+english;
  53.           }
  54.           public int compareTo(student s){
  55.                      int num = new Integer(this.sum).compareTo(s.sum);
  56.                      if(num==0){
  57.                              return this.name.compareTo(s.name);
  58.                      }
  59.                      else{
  60.                              return num;
  61.                      }
  62.           }
  63.           public String toString(){
  64.                      return name+","+math+","+ch+","+english;
  65.           }
  66. }

  67.          
复制代码
程序没报错,为什么运行不了啊?

评分

参与人数 1黑马币 +1 收起 理由
杨佳名 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马