当前位置: 代码迷 >> Java相关 >> (求助)看看我的程序在输出时有什么问题
  详细解决方案

(求助)看看我的程序在输出时有什么问题

热度:138   发布时间:2007-11-26 16:04:52.0
(求助)看看我的程序在输出时有什么问题
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部分在输出时只能输出一个元素,怎么回事?
请大家帮忙看看,先谢过了
            
            
    }
}
搜索更多相关的解决方案: 输出  时有  

----------------解决方案--------------------------------------------------------
并不是while只能输出一个元素 而是你自己定义的compareTo方法有问题,按照你的代码,那么只有在英语成绩相等的时候才能添加进TreeMap
public int  compareTo(Object b)
     {
         TheKey st=(TheKey)b;
         if(this.mark==st.mark)
         {
             return -1;    //改成return = 0;
         }
         else
         {
             return  (this.mark-mark);     //改成return = -1;或者1
         }
     }
按我改的你试试
再改改你的逻辑

[[italic] 本帖最后由 StarScar 于 2007-11-26 21:18 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
收藏,有待研究。
----------------解决方案--------------------------------------------------------
提点小意见啊,是不是声明变量的时候只能在方法中初始化啊,
----------------解决方案--------------------------------------------------------
看你在哪儿声明的变量了
----------------解决方案--------------------------------------------------------
  相关解决方案