import java.util.*;
public class scanner
{
static boolean flag=true;
static int x;
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
while(flag)
{
try
{
System.out.println("key in a integer");
x=input.nextInt();
flag=false;
}
catch (InputMismatchException e)
{
System.out.println("Only Integer,try again");
}
}
System.out.println(x);
}
}
当输入一个非整数字符时,程序陷入了死循环,求教
public class scanner
{
static boolean flag=true;
static int x;
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
while(flag)
{
try
{
System.out.println("key in a integer");
x=input.nextInt();
flag=false;
}
catch (InputMismatchException e)
{
System.out.println("Only Integer,try again");
}
}
System.out.println(x);
}
}
当输入一个非整数字符时,程序陷入了死循环,求教