【发布时间】:2017-08-31 10:51:43
【问题描述】:
当我在 while 循环中使用 try and catch 块时,我的程序有一个无限循环。
import java.util.*;
class Try
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
while(true)
{
try {
System.out.println("Enter a no ");
int s=sc.nextInt();
} catch(Exception e) {
System.out.println("Invalid input try again");
}
}
}
}
当我输入一个整数时,它运行良好并要求另一个输入,但是当我输入一个字符时,它会进入无限循环。为什么会这样?
【问题讨论】:
-
它是继续打印还是只是挂起?
-
它将在无限循环中打印什么
-
它像无限循环一样,不断打印“输入一个否”“Invlide input try again”
标签: java while-loop try-catch infinite-loop