一个基础问题。

q1531 2008-05-03 06:03:49
import java.util.*;
class Test {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int a=0;
while(true){
try{
a=input.nextInt();
break;
}
catch(Exception e){System.out.println("输入错误!");}
}
System.out.print(a);
}
}
//////////////////
在各问题在C中也遇到过类似的,但一直不知道是什么原因。
在这里输入的是非数字哪么就会产生异常。
输出一段字符,作为提示。因为while(true),所以将一直循环到输入的是数字为止,我的理解就是这样。
但执行的时候如果输入的不是数字,哪么就会一直执行System.out.println("输入错误!");
为什么?
...全文
46 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
q1531 2008-05-03
  • 打赏
  • 举报
回复
呵呵知道是什么原因了。原来nextInt()接收换行。

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧