同志们,谁能帮我看看这个为啥总是异常么,非常感谢
结果是正确的,但是总显示java.util.NoSuchElementException: No line found,说这行有错误:String str=sc.nextLine();- public class Test9 {
- public static void main(String[] args) {
- Scanner sc=null;
- while(true){
- sc =new Scanner(System.in);
- String str=sc.nextLine();
- int a=0;
- if(isOctNumers(str)){
- a=Integer.valueOf(str);
- }
- else {
- System.out.println("输入不正确,请重新输入");
- continue;
- }
- System.out.println(toBinary(a));
- sc.close();
- }
-
- }
- private static String toBinary(int decimal) {
- StringBuilder sb=new StringBuilder();
- int x=0;
- while(decimal !=0){
- x=decimal %2;
- decimal =(int)(decimal/2);
- sb.append(x);
- }
- sb.reverse();
- return sb.toString();
-
- }
- private static boolean isOctNumers(String str) {
-
- try{
- Integer.parseInt(str);
- return true;
- }
- catch(NumberFormatException e){
- return false;
-
- }
-
- }
- }
复制代码
|
|