黑马程序员技术交流社区

标题: 实在想不懂,为什么只循环一次? [打印本页]

作者: 油炸电视剧    时间: 2015-5-16 12:24
标题: 实在想不懂,为什么只循环一次?
题目是这样的:
1.现有Student类,属性有name, age, score(int类型).
        要求 : 按照学生的分数排序, 分数大的童鞋在前面, 如果分数相同, 那么年龄小的在前面, 如果分数年龄都相同, 则按照姓名(英文的即可)的字典顺序排序.要求:不允许在描述类上写排序规则.
下面是给大家学生.
Student("Tom",24, 89)
Student("Robin",32, 99));
Student("Jerry",24, 99));
Student("Lili",23, 87));
Student("Jack",22, 87));
Student("LiLei",25, 95));
Student("Robin",32 ,99));
下面是我的代码:
  1. package test;

  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.TreeSet;

  5. public class Test1
  6. {
  7.         public static void main(String[] args)
  8.         {
  9.                 TreeSet set=new TreeSet(new MyComparator());
  10.                
  11.                 Student s1=new Student("Tom",24, 89);
  12.                 Student s2=new Student("Robin",32, 99);
  13.                 Student s3=new Student("Jerry",24, 99);
  14.                 Student s4=new Student("Lili",23, 87);
  15.                 Student s5=new Student("Jack",22, 87);
  16.                 Student s6=new Student("LiLei",25, 95);
  17.                 Student s7=new Student("Robin",32 ,99);
  18.                
  19.                 set.add(s1);
  20.                 set.add(s2);
  21.                 set.add(s3);
  22.                 set.add(s4);
  23.                 set.add(s5);
  24.                 set.add(s6);
  25.                 set.add(s7);
  26.                
  27.                 for(Iterator ite=set.iterator();ite.hasNext();)
  28.                 {
  29.                         Student s=(Student) ite.next();
  30.                         System.out.println("成绩:"+s.getScore()+"年龄:"+s.getAge()+"姓名:"+s.getName());
  31.                 }
  32.         }

  33. }

  34. class Student
  35. {
  36.         String name;
  37.         int age;
  38.         int score;

  39.         public Student(String name, int age, int score)
  40.         {
  41.                 this.age = age;
  42.                 this.name = name;
  43.                 this.score = score;
  44.         }

  45.         public String getName()
  46.         {
  47.                 return name;
  48.         }

  49.         public void setName(String name)
  50.         {
  51.                 this.name = name;
  52.         }

  53.         public int getAge()
  54.         {
  55.                 return age;
  56.         }

  57.         public void setAge(int age)
  58.         {
  59.                 this.age = age;
  60.         }

  61.         public int getScore()
  62.         {
  63.                 return score;
  64.         }

  65.         public void setScore(int score)
  66.         {
  67.                 this.score = score;
  68.         }
  69. }

  70. class MyComparator implements Comparator
  71. {

  72.         public int compare(Object o1, Object o2)
  73.         {
  74.                 Student s1 = (Student) o1;
  75.                 Student s2 = (Student) o2;

  76.                 String n1 = s1.getName();
  77.                 String n2 = s2.getName();
  78.                 int l=Math.max(n1.length(), n2.length());

  79.                 int a1 = s1.getAge();
  80.                 int a2 = s2.getAge();

  81.                 int sc1 = s1.getScore();
  82.                 int sc2 = s2.getScore();

  83.                 if (sc1 > sc2)
  84.                 {
  85.                         return -1;
  86.                 }
  87.                 else if (sc1 == sc2)
  88.                 {
  89.                         if (a1 > a2)
  90.                         {
  91.                                 return 1;
  92.                         }
  93.                         else if (a1 == a2)
  94.                         {
  95.                                 for (int i = 0; i < l-1; i++)
  96.                                 {
  97.                                         if (n1.charAt(i) > n2.charAt(i))
  98.                                         {
  99.                                                 return 1;
  100.                                         }
  101.                                         if (n1.charAt(i) == n2.charAt(i))
  102.                                         {
  103.                                                 return 0;
  104.                                         }
  105.                                         return -1;
  106.                                 }
  107.                         }
  108.                         return -1;
  109.                 }
  110.                 return 1;

  111.         }
  112. }
复制代码


就是判断姓名的时候那里只会循环一次,而且只会输出6个结果。这是为什么?求大神指点以及改进!
作者: xingfeichen    时间: 2015-5-16 19:16
额 。。一句注释都没有,老毕说了,这不是好习惯
作者: 我干阿衰    时间: 2015-5-17 00:14
本帖最后由 我干阿衰 于 2015-5-17 00:17 编辑

你这代码,我也是醉了,太乱了!!!而且除了迭代外好像也没有别的循环了吧?哪里还有名字的循环?你这写的真是太复杂了





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2