本帖最后由 whmwg 于 2011-07-06 02:15:46 编辑

解决方案 »

  1.   

    int i  = 0 ;
    int j = 0 ;
    while (true){

    try{
    if ( j ==3) {
    break ;
    }
    i++ ;
    System.out.println( i );
    if ( j ==5) {
    break ;
    }
    if (i > 5){
    j++ ;
    throw new Exception("aaa") ;
    }
    }catch(Exception e){
    System.out.println(e.getMessage());
    i  = 0 ;
    System.out.println("+++++++++++++++++++++++++++++++++++++");
    }
    }
    结果:1
    2
    3
    4
    5
    6
    aaa
    +++++++++++++++++++++++++++++++++++++
    1
    2
    3
    4
    5
    6
    aaa
    +++++++++++++++++++++++++++++++++++++
    1
    2
    3
    4
    5
    6
    aaa
    +++++++++++++++++++++++++++++++++++++
      

  2.   

    我感觉直接和数字比较判断就可以了。没必要非得整个try/catch。
      

  3.   

    因为你try里没运行到break就出现异常,就不会执行break语句
      

  4.   

    因为从没执行到过break语句,所以死循环。
    既然死循环了,说明异常被你捕捉到了,说明确实是InputMismatchException,说明你输入的类型不对,不是整数。输入整数就Ok了
      

  5.   

    两句话:
    1、不要在循环语句中进行try-catch;
    2、异常机制的执行流程是具有不确定性的;
      

  6.   

    只能说是没法执行到break语句了
      

  7.   

    写的啰嗦,多用debug和System.out.println();可以解决很多问题的。
      

  8.   

    估计是这句出现的异常:int count = input.nextInt();那么程序就直接跳到catch的部分了,break根本就没有跑到,所以就是个死循环.
      

  9.   

    break没有执行到,错误肯定都被catch到了,所以才死循环。不知道楼主的目的何在??
      

  10.   

    参考一下:
    Scanner input = new Scanner(System.in);
            String regex = "^\\d*$";
            int count = 0;
            while(true)
            {
                System.out.println("请输入商品个数:");
                String s = input.next();
                if(s.matches(regex))
                {
                    System.out.println(s);
                    count = Integer.parseInt(s);
                    break;
                }
                
            }
            
            System.out.println("count = " + count);
      

  11.   

    楼上的都说的很对啊,确实是没到break就catch了,然后就死循环
    当然whlie中也不是不能用try块,但是请一定一并用上finally
    因为把try块放在循环里,就创造了一个“程序继续执行之前必须要达到”的条件。
    那么加入例如finally{
        if(count<=10)break;
    }就可以让循环尝试一定次数,从而不进入死循环。
    PS:专门翻了下《Thinking in Java》,不知道你有没听懂啊,哈哈~