C语言中测试多组数据时while(scanf("%d",&a)!=EOF),我要用JAVA该怎么做呢?
答案:2 悬赏:50 手机版
解决时间 2021-04-26 12:55
- 提问者网友:山高云阔
- 2021-04-26 10:00
C语言中测试多组数据时while(scanf("%d",&a)!=EOF),我要用JAVA该怎么做呢?
最佳答案
- 五星知识达人网友:酒者煙囻
- 2021-04-26 10:14
你应该使用 Scanner 的 hasNext() 方法来判断输入流中是否还有数据,比如这样做:
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
System.out.println(sc.nextInt());
}追问
可以了~~~
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
System.out.println(sc.nextInt());
}追问
可以了~~~
全部回答
- 1楼网友:举杯邀酒敬孤独
- 2021-04-26 10:52
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in); //打开输入流
int a = 0, b = 0;
while (true) { //无限循环
try{
a=scanner.nextInt(); //等待输入数字
b=scanner.nextInt(); //等待输入数字
}catch(Exception e){//捕获异常,非数字
System.out.println("wrong input!");
scanner.close();//关闭输入流
System.exit(0);//程序安全退出
}
System.out.println(a+b); //打印结果
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯