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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

我用eclipse显示报错为何还可以编译成功呢有点不理解请教一下大家代码如下:import java.util.*;
class Student implements Comparable<Student>
{
        private String name;
        private int age;
        Student(String name,int age)
        {
                this.name=name;
                this.age=age;
        }
        public int compareTo(Student s)
        {
                int num=new Integer(this.age).compareTo(new Integer(s.age));
                if(num==0)
                {
                        return this.name.compareTo(s.name);
                        return num;
                }
        }
        public int hashCode()
        {
                return name.hashCode()+age*43;
        }
        public boolean equals(Object obj)
        {
                if(!(obj instanceof Student))
                        throw new ClassCastException("类型不匹配");
                Student s=(Student)obj;
                return this.name.equals(s.name)&&this.age==s.age;
               
        }
       
       
        public String getName()
        {
                return name;
        }
        public int getAge()
        {
                return age;
        }
        public String toString()
        {
                return name+"::"+age;
        }
}
class MapTest
{
        public static void main(String[] args)
        {
                HashMap<Student,String>hm=new HashMap<Student,String>();
                hm.put(new Student("lisi1",21), "beijin");
                hm.put(new Student("lisi2",22), "shanghai");
                hm.put(new Student("lisi3",23), "nanjing");
                hm.put(new Student("lisi4",24), "wuhan");
               
                //第一种取出方式 keySet
                Set<Student>keySet=hm.keySet();
                Iterator<Student>it=hm.keySet().iterator();
                while(it.hasNext())
                {
                        Student stu=it.next();
                        String addr=hm.get(stu);
                        System.out.println(stu+"::"+addr);
                }
               
                //第二种取出方式 entrySet
                Set<Map.Entry<Student,String>>entrySet=hm.entrySet();
                Iterator<Map.Entry<Student, String>>iter=entrySet.iterator();
                while(iter.hasNext())
                {
                        Map.Entry<Student,String>me=iter.next();
                        Student stu=me.getKey();
                        String addr=me.getValue();
                        System.out.println(stu+"........"+addr);                                                                                                                                               
                }
        }

}       
报错的是

this method mest  return a result of  type int

unreachable code

未命名.jpg (24.28 KB, 下载次数: 9)

未命名.jpg

0010.jpg (42.7 KB, 下载次数: 5)

0010.jpg

评分

参与人数 1技术分 +1 收起 理由
黄炳期 + 1

查看全部评分

3 个回复

倒序浏览
程序是没有问题的,它提示的问题是这个方法最终返回的是int,代码执行不到,我想你下面有两个方法,都被调用,函数返回时,不知道要返回哪个类型,你把其中一个方法隐藏掉估计就行了,这种事情我原来也出现过,因为你用Edipulst使用文件夹方式存储,没问题,而eclipse文件你都存储方通过一文件下,就一产生问题。

评分

参与人数 1技术分 +1 收起 理由
黄炳期 + 1

查看全部评分

回复 使用道具 举报
靓仔 发表于 2013-10-30 12:02
程序是没有问题的,它提示的问题是这个方法最终返回的是int,代码执行不到,我想你下面有两个方法,都被调 ...

我还是不太理解,有错为何能运行呢?而且还是结果
回复 使用道具 举报
就是说这个错误不影响你运行,那个代码相当于一句永远执行不到的语句
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马