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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© shdosh 中级黑马   /  2015-8-27 21:22  /  257 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1.         public static Set<Student> getScore(Comparator<Student> cmp)throws IOException{

  2.                 Set<Student> stuSet = null;//盲点:如果写在if里面,下面会出现stuSet变量找不到的错误,因为判断语句可能执行不到。
  3.                 if(cmp == null)
  4.                         /*Set<Student>*/ stuSet = new TreeSet<Student>();
  5.                 else
  6.                         stuSet = new TreeSet<Student>(cmp);
  7.                 BufferedReader bufIn =  new BufferedReader(new InputStreamReader(System.in));
  8.                
  9.                 String str = null;

  10.                 while((str = bufIn.readLine())!=null){
  11.                         if(str.equals("over"))
  12.                                 break;
  13.                         String[] stuInfo = str.split(",");
  14.                        
  15.                         Student stu = new Student(stuInfo[0],Integer.parseInt(stuInfo[1]),Integer.parseInt(stuInfo[2]),Integer.parseInt(stuInfo[3]));
  16.                        
  17.                         stuSet.add(stu);
  18.                 }
  19.                 return stuSet;

  20.         }
复制代码

3 个回复

倒序浏览
如果把Set的定义放在if判断中会出现stuSet变量找不到符号的错误,难道是编译器认为判断语句可能会执行不到,所以认为找不到符号?
回复 使用道具 举报
如果定义在if里面,你确定在if里面定义的局部变量,到下面的whlie循环能用?
回复 使用道具 举报
chensheng06 发表于 2015-8-27 21:47
如果定义在if里面,你确定在if里面定义的局部变量,到下面的whlie循环能用? ...

Set<Student> stuSet = null;写在if外面能用,写在if里面就不能用了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马