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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 当我遇上你 中级黑马   /  2015-1-10 14:06  /  1078 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1.         public static  class Student
  2.         {
  3.                 double chinese;
  4.                 double math;
  5.                 double english;
  6.                 double sum;
  7.             String name;
  8.                 public Student(double chinese, double math, double english, double sum,
  9.                                 String name)
  10.                 {
  11.                         this.chinese = chinese;
  12.                         this.math = math;
  13.                         this.english = english;
  14.                         this.sum = sum;
  15.                         this.name = name;
  16.                 }
  17.                 @Override
  18.                 public String toString()
  19.                 {
  20.                         return String.format("%s\t\t%2$.1f\t\t%3$.1f\t\t%4.1f\t\t%5$.1f", name,chinese,math,english,sum);
  21.                 }
  22.                 public static void main(String[] args)throws Exception
  23.                 {
  24.                         Scanner scanner = new Scanner(System.in);
  25.                         LinkedList<Student> linkedList = new LinkedList<Student>();
  26.                         System.out.println("请按以下格式录入学生信息:name,30,30,30(姓名,语文,数学,外语)<按回车键结束>:");
  27.                         while(scanner.hasNextLine())
  28.                         {
  29.                                 String str = scanner.nextLine();
  30.                                 if(!str.equals(null))
  31.                                 {
  32.                                         String[] inf=str.split("\\,");
  33.                                         String name=inf[0];
  34.                                         double chinese=Double.parseDouble(inf[1]);
  35.                                         double math=Double.parseDouble(inf[2]);
  36.                                         double english=Double.parseDouble(inf[3]);
  37.                                         double sum=chinese+math+english;
  38.                                         Student student = new Student(chinese, math, english, sum, name);
  39.                                         linkedList.add(student);
  40.                                 }
  41.                         }
  42.                         scanner.close();
  43.                         Collections.sort(linkedList, new MyComparator());   \\  <<----------------
  44.                         System.setOut(new PrintStream("stu.txt"));
  45.                         System.out.println("姓名\t\t语文\t\t数学\t\t外语\t\t总分");
  46.                         for(Student stu:linkedList)
  47.                         {
  48.                                 System.out.println(stu.toString());
  49.                         }
  50.                 }
  51.                
  52.                 class MyComparator implements Comparator<Student>
  53.                 {
  54.                         @Override
  55.                         public int compare(Student s1,Student s2)
  56.                         {
  57.                                 if (s1.sum>s2.sum)
  58.                                 {
  59.                                         return (int) (s1.sum-s2.sum);
  60.                                 } else if(s1.sum-s2.sum<0)
  61.                                 {
  62.                                         return (int) (s1.sum-s2.sum);
  63.                                 }
  64.                                 else return 0;
  65.                                
  66.                         }
  67.                 }
  68.         }
复制代码

代码还没有调试,不知道写的比较方法是否真确,错误提示出现在我调用比较方法的那一行,我做了标记

2 个回复

倒序浏览
因为Mycompatator为内部类,而你在静态方法中调用了内部类,所以要么内部类变为静态类 static Mycomparator,要么先new Student().new Mycomparator()。
还有你的程序判断读取完毕时错误的,应该这样写:
  1.                         if(!str.equals(""))
  2.                         {
  3.                                 String[] inf=str.split("\\,");
  4.                                 String name=inf[0];
  5.                                 double chinese=Double.parseDouble(inf[1]);
  6.                                 double math=Double.parseDouble(inf[2]);
  7.                                 double english=Double.parseDouble(inf[3]);
  8.                                 double sum=chinese+math+english;
  9.                                 Student student = new Student(chinese, math, english, sum, name);
  10.                                 linkedList.add(student);
  11.                         }
  12.                         else{
  13.                                 break;
  14.                         }
复制代码
回复 使用道具 举报
kerner 发表于 2015-1-10 15:54
因为Mycompatator为内部类,而你在静态方法中调用了内部类,所以要么内部类变为静态类 static Mycomparator ...

恩恩,后来我弄懂了,不过还是谢谢你解释 的那么详细
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马