为什么这个While循环卡在这个无限循环中?

时间:2019-10-15 13:20:56

标签: java while-loop

有人知道为什么这段代码陷入无限循环吗?在没有输入整数的情况下运行后,它将无限重复“请输入整数”

class ExceptionHandling {
    public static void main(String[] args) {
        int foo= getInteger();
    }

    public static int getInteger() {
        Scanner in = new Scanner(System.in);

        while(true)
        {
            int delta = 0;
            try
            {
                delta = in.nextInt();
                return delta;
            }
            catch (Exception ex)
            {
                System.out.println("Please enter an integer");
            } 
        }
    }
}

0 个答案:

没有答案