代码如下
Scanner sc = new Scanner(System.in);
int[] num = new Int[4];for(int i = o ;i < 4;i ++)
{
try{
num[i] = sc.nextInt();
}catch(Exception e)
{
System.out.println("error");
}
}结果当在控制台输入 字符“a” 的时候,输出了4条“error” 。调试时,一到 num[i] = sc.nextInt();,就直接跳到了catch语句。说明缓存中 sc.nextIn() 保存了 “a” 。
我想问,如何在异常处理后清空 sc.nextIn() 缓存,继续进行正常的循环。
Scanner sc = new Scanner(System.in);
int[] num = new Int[4];for(int i = o ;i < 4;i ++)
{
try{
num[i] = sc.nextInt();
}catch(Exception e)
{
System.out.println("error");
}
}结果当在控制台输入 字符“a” 的时候,输出了4条“error” 。调试时,一到 num[i] = sc.nextInt();,就直接跳到了catch语句。说明缓存中 sc.nextIn() 保存了 “a” 。
我想问,如何在异常处理后清空 sc.nextIn() 缓存,继续进行正常的循环。
,第二种是nextInt(), 而参数不能转化为int,比如你输入的"a"2 你的catch已经处理了异常,你的输出会继续运行的,4个error就说明了这个问题,你可以控制台输入a 123 456输入这3个参数看看异常是什么。
a转换成int的时候出现异常
你只有一个参数 在进行后面三次sc.nextInt(); 当然也会产生异常.