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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨希 中级黑马   /  2014-9-18 12:15  /  543 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

同志们,谁能帮我看看这个为啥总是异常么,非常感谢
结果是正确的,但是总显示java.util.NoSuchElementException: No line found,说这行有错误:String str=sc.nextLine();
  1. public class Test9 {

  2.         public static void main(String[] args) {
  3.                 Scanner sc=null;
  4.                 while(true){
  5.                         sc =new Scanner(System.in);
  6.                         String str=sc.nextLine();
  7.                         int a=0;
  8.                         if(isOctNumers(str)){
  9.                                 a=Integer.valueOf(str);
  10.                         }
  11.                         else {
  12.                                 System.out.println("输入不正确,请重新输入");
  13.                                 continue;
  14.                         }
  15.                         System.out.println(toBinary(a));
  16.                         sc.close();
  17.                 }
  18.                

  19.         }

  20.         private static String toBinary(int decimal) {
  21.                 StringBuilder sb=new StringBuilder();
  22.                 int x=0;
  23.                 while(decimal !=0){
  24.                         x=decimal %2;
  25.                         decimal =(int)(decimal/2);
  26.                         sb.append(x);
  27.                 }
  28.                 sb.reverse();
  29.                 return sb.toString();
  30.                
  31.         }

  32.         private static boolean isOctNumers(String str) {
  33.                
  34.                 try{
  35.                         Integer.parseInt(str);
  36.                         return true;
  37.                 }
  38.                 catch(NumberFormatException e){
  39.                         return false;
  40.                         
  41.                 }
  42.                
  43.         }

  44. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马