Core Java 书中讲集合的一段代码,有些疑问

口香糖先森 2014-06-27 10:48:08

package execise.coreJava;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class SetTest {
public static void main(String[] args) {
Set<String> words = new HashSet<>();

long totalTime = 0;

Scanner in = new Scanner(System.in);

while(in.hasNext()) {

String word = in.next();

long callTime = System.currentTimeMillis();

words.add(word);

callTime = System.currentTimeMillis() - callTime;

totalTime += callTime;

}

Iterator<String> iter = words.iterator();

for(int i = 1; i <= 20 && iter.hasNext(); i++)
System.out.println(iter.next());
System.out.println(". . .");
System.out.println(words.size() + " distinct words." + totalTime + " milliseconds.");
}
}



英文版的书中说:The program reads all words from the input and adds them to the hash set. It
then iterates through the unique words in the set and finally prints out a count.
(Alice in Wonderlandhas 5,909 unique words, including the copyright notice at the
beginning.) The words appear in random order.

但是我执行完这个代码 怎么都执行不到 30 行之后的输出,求解 是不是程序就是这样的啊,还是我没看明白?
...全文
119 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
口香糖先森 2014-06-28
  • 打赏
  • 举报
回复
引用 1 楼 lg1544650023 的回复:
你需要按ctrl+z来结束输入,这样才能跳出while循环 (但是你的程序意思我不怎么明白)
感谢,就是你说的这个情况。
sleeplacker 2014-06-28
  • 打赏
  • 举报
回复
你需要按ctrl+z来结束输入,这样才能跳出while循环 (但是你的程序意思我不怎么明白)

62,620

社区成员

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

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