牛客网编程之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

相关推荐

浩浩没烦恼:一二面加起来才一个小时? 我一面就一个小时多了
点赞 评论 收藏
分享
程序员牛肉:继续沉淀吧同学,你这就是纯纯的流水线产品。 差不多的学历+两个烂大街项目。自身学历又不行,现在找啥实习呢。有点太浮躁了。多花点心思搞搞ai,开源和八股。这比你这段时间捣鼓一段小厂实习要好得多;
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

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