牛客网编程之java输入输出的坑

```
import java.util.;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
Set set = new TreeSet();
for (int i = 0; i < n; i++) {
set.add(sc.nextInt());
}
/*Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
/
for(int iter:set){
System.out.println(iter);
}
}

}

}

为什么上面可以通过,下面这样没法通过?

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Set set = new TreeSet();
while(sc.hasNext()){
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
set.add(sc.nextInt());
}
}
for(int iter:set){
System.out.println(iter);
}
}
}

#笔试题#
全部评论
是不是用了两次nextInt
点赞 回复 分享
发布于 2021-04-09 09:38

相关推荐

10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
影04714:把图书管理系统那个项目经验内容适当的减少掉,然后改成据为己有不要说团队项目,因为图书管理系统这类常见的谁来了都能独立写出来,提问能圆过来即可
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务