public class ScannerTest {    public static void main(String...args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("input a number:");
        while(true){
            int score = 0;
            try{
                score = scanner.nextInt();
            }catch(Exception e){
//                e.printStackTrace();
                System.out.println("illegal number");
                continue;//这个地出现死循环
            }
            
            switch(score/10){
                case 10:
                case 9:
                    System.out.println("Excellent");
                break;
                case 8:
                    System.out.println("Good");
                break;
                case 7:
                case 6:
                    System.out.println("Pass");
                break;
                default:
                    System.out.println("Fail");
                break;
            }
        }
    }
}
当用户输入的不是数字时,会死循环,请问为什么?

解决方案 »

  1.   


    因为你写了continue 写break就不会了
      

  2.   

    所以不要在while里面用true,可以设置一个boolean型的变量,当某一条件不满足时可以置为false。这样就不会出现死循环了!通常情况下while里面都不用常量true
      

  3.   

    下次的循环的时候,scanner并没有挂起等待输入了,我也不知道为什么
    把定义scanner放到循环体里就得了import java.util.*;
    public class ScannerTest {    public static void main(String...args){
            
            System.out.print("input a number:");
            while(true){
                Scanner scanner = new Scanner(System.in);
                int score = 0;
        boolean flag = true;
                try{
                    score = scanner.nextInt();
                }catch(Exception e){
                    System.out.println("illegal number");
                    continue;
                }
                switch(score/10){
                    case 10:
                    case 9:
                        System.out.println("Excellent");
                    break;
                    case 8:
                        System.out.println("Good");
                    break;
                    case 7:
                    case 6:
                        System.out.println("Pass");
                    break;
                    default:
                        System.out.println("Fail");
                    break;
                }
            }
        }
    }
      

  4.   

    楼上说的对另外给楼主个建议
    可以用BufferedReader
    代替Scanner
    以前做POJ的题目
    同一个程序,我用Scanner的,一看运行时间,2000多ms
    换成BufferedReader,立马变成了300多ms
      

  5.   

    多谢楼上两位,  可以为什么scanner不等待用户再次输入就死循环呢?
      

  6.   

    一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器。 Scanner 使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白匹配。然后可以使用不同的 next 方法将得到的标记转换为不同类型的值。 例如,以下代码使用户能够从 System.in 中读取一个数:      Scanner sc = new Scanner(System.in);
         int i = sc.nextInt();
     再看一个例子,以下代码使 long 类型可以通过 myNumbers 文件中的项分配:       Scanner sc = new Scanner(new File("myNumbers"));
          while (sc.hasNextLong()) {
              long aLong = sc.nextLong();
          }扫描器还可以使用不同于空白的分隔符。下面是从一个字符串读取若干项的例子:      String input = "1 fish 2 fish red fish blue fish";
         Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
         System.out.println(s.nextInt());
         System.out.println(s.nextInt());
         System.out.println(s.next());
         System.out.println(s.next());
         s.close(); 输出为:      1
         2
         red
         blue 以下代码使用正则表达式同时解析所有的 4 个标记,并可以产生与上例相同的输出结果:      String input = "1 fish 2 fish red fish blue fish";
         Scanner s = new Scanner(input);
         s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
         MatchResult result = s.match();
         for (int i=1; i<=result.groupCount(); i++)
             System.out.println(result.group(i));
         s.close(); 扫描器所使用的默认空白分隔符通过 Character.isWhitespace 来识别。不管以前是否更改,reset() 方法将把扫描器分隔符的值重置为默认空白分隔符。 扫描操作可能被阻塞,而等待信息的输入。 
      

  7.   

    continue不会跳出循环只会跳过本次循环,要用break来跳出循环。
      

  8.   

    怎么这么多人在说continue和break区别大家运行下这个程序就应该知道我想干嘛以及我想问什么了用户在输入非数字的时候,会打印出illegal number,然后用户可以再重新输入。并不是要跳出程序的执行
      

  9.   

    sc.nextInt();当输入非数字时,
    并没有做光标移动操作(没有跳过这个非数字),
    然后跳到了catch块中,
    继续下一次循环,
    由于上一次的光标没有跳过非数字,
    所以继续sc.nextInt();
    得到的还是上次输入的非数字,
    出现了死循环。
     while(true){
                Scanner scanner = new Scanner(System.in);
                int score = 0;如果像上面的代码,将扫描器在循环内部初始化,那每次sc都会空的,然后监听用户的输入,
    就算遇到异常,下次循环时会重新初始化,就不会遇到死循环了!希望有高人解答。。我也乱猜的
      

  10.   

    看这个程序就很烂,switch用的很差
      

  11.   


    import java.util.Scanner;
    public class ScannerTest {    public static void main(String...args){
            Scanner scanner = new Scanner(System.in);
            System.out.println("input a number:");
            while(true){
                int score = 0;
                try{
                    score = scanner.nextInt();
                }catch(Exception e){
    //                e.printStackTrace();
                    System.out.println("illegal number");
                    String temp = scanner.next();//早上理解错了,在这里加上这行就好了,如果没有这一行
                    //,当你输入的不是数字的时候,就会执行catch里面的代码,continue到循环开始处继续,下次读取的时候
                    //还是前面输入的非数字字符,所以继续抛出异常,这样就成两个一个死循环,所以可以在发生异常后处理掉
                    //输入的非数字字符
                    continue;//这个地出现死循环
                }
                
                switch(score/10){
                    case 10:
                    case 9:
                        System.out.println("Excellent");
                    break;
                    case 8:
                        System.out.println("Good");
                    break;
                    case 7:
                    case 6:
                        System.out.println("Pass");
                    break;
                    default:
                        System.out.println("Fail");
                    break;
                }
            }
        }
    }
    程序中做了注释!
      

  12.   

    楼主的程序很简单你只要你那个flag就是用来标记退出的所以只要这样改就可以了
    把flag作为main方法的全局变量把他放在while(flag)中再在每个case语句中break之前加入flag=false;
    这样就可以跳出循环了具体代码如下public class ScannerTest {    public static void main(String...args){
         boolean flag = true;
            System.out.print("input a number:");
            while(flag){
                Scanner scanner = new Scanner(System.in);
                int score = 0;
           
                try{
                    score = scanner.nextInt();
                }catch(Exception e){
                    System.out.println("illegal number");
                    continue;
                }
                switch(score/10){
                    case 10:
                    case 9:
                        System.out.println("Excellent");
                        flag=false;
                    break;
                    case 8:
                        System.out.println("Good");
                        flag=false;
                    break;
                    case 7:
                    case 6:
                        System.out.println("Pass");
                        flag=false;
                    break;
                    default:
                        System.out.println("Fail");
                        flag=false;
                    break;
                }
            }
        }
    }
      

  13.   

    import java.util.Scanner;public class ScannerTest {    public static void main(String...args){
            Scanner scanner = new Scanner(System.in);
            System.out.println("input a number:");
            while(true){
                String scoreStr;
                int  score;
                try{
                 scoreStr = scanner.next();
                    score=Integer.parseInt(scoreStr);
                }catch(Exception e){
    //                e.printStackTrace();
                    System.out.println("illegal number");
                    continue;
                }
                
                switch(score/10){
                    case 10:
                    case 9:
                        System.out.println("Excellent");
                    break;
                    case 8:
                        System.out.println("Good");
                    break;
                    case 7:
                    case 6:
                        System.out.println("Pass");
                    break;
                    default:
                        System.out.println("Fail");
                    break;
                }
            }
        }
    }1 这样可以运行.我觉得挺好的
    2 程序建议有个提示请输入[0-100]之间的一个数
    3 大于100的数应该提示illegal而不是走到switch的default "Fail"
      

  14.   


    多謝,我也試過通過parseInt不會死徨環,你的提議也很好,謝謝。謝謝大家,結帖了