回 帖 发 新 帖 刷新版面

主题:[原创](求助)看看我的程序在输出时有什么问题(已解决)

import java.util.*;
class TheKey implements Comparable
{
     int  mark=0;
     TheKey(int mark)
     {
         this.mark=mark;
     }
     
     public int  compareTo(Object b)
     {
         TheKey st=(TheKey)b;
         if(this.mark==st.mark)
         {
             return -1;
         }
         else
         {
             return  (this.mark-mark);
         }
     }
}

class Student
{
    String name;
    int  englishMark;
    int  mathMark;
    
    Student(String name, int e, int m)
    {
        englishMark=e;
        mathMark=m;
        this.name=name;
    }
}

public class ExTreeMap
{
    public static void main(String[] args)
    {
        Student st[]=new Student[5];
        for(int i=0; i<5; i++)
        {
            Scanner input=new Scanner(System.in);
            System.out.println("请输入第"+(i+1)+"学生的信息:");
            System.out.printf("输入姓名:");
            String name=input.nextLine();
            System.out.printf("输入英语成绩:");
            int e=input.nextInt();
            System.out.printf("输入数学成绩:");
            int m=input.nextInt();
            st[i]=new Student(name, e, m);       
                
        }
        
        TreeMap<TheKey, Student>  treemap=
            new TreeMap<TheKey, Student>(new Comparator<TheKey>()
                                                                    {
                                                                         public int  compare(TheKey a, TheKey b)
                                                                         {
                                                                               return  a.compareTo( b);
                                                                         }
                                                                    });
          treemap.put(new TheKey(st[0].englishMark),st[0]); 
          treemap.put(new TheKey(st[1].englishMark),st[1]); 
          treemap.put(new TheKey(st[2].englishMark),st[2]); 
          treemap.put(new TheKey(st[3].englishMark),st[3]); 
          treemap.put(new TheKey(st[4].englishMark),st[4]); 
          
          Collection<Student> collection=treemap.values();
          Iterator<Student>  iter=collection.iterator();
         while(iter.hasNext())
         { 
           Student storder=iter.next();
           System.out.println("姓名:"+storder.name+"  英语成绩"+storder.englishMark);
         }    

最后的这个while部分在输出时只能输出一个元素,怎么回事?
请大家帮忙看看,先谢过了
             
             
    }
}

回复列表 (共1个回复)

沙发

没人回答我就自己解决吧!
import java.util.*;
class TheKey implements Comparable
{
     int  mark=0;
     TheKey(int mark)
     {
         this.mark=mark;
     }
     
     public int  compareTo(Object b)
     {
         TheKey st=(TheKey)b;
         if(this.mark==st.mark)
         {
             return -1;
         }
         else
         {
             return  (this.mark-mark);   ->这里改为 return  (this.mark-st.mark);  
                                           因为this.mark是等于mark,所以排序的时候每次都
                                           会覆盖前面的数,所以只会输出最后的那个 
         }
     }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册