能麻烦帮忙看下这个程序为什么不能运行吗

cat_Honey 2008-06-07 02:47:25
第一次来这里提问,是个刚刚学习java的菜鸟,希望得到您的帮助
下面的这个程序不知道为什么运行不出来 用的是eclipse,多谢

package day607;
import java.util.*;
public class TestSet {

public static void main(String[] args) {
Set<String> words=new HashSet<String> ();
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;1
totalTime+=callTime;
}
Iterator<String> iter=words.iterator();
for(int i=1;i<=20;i++){
System.out.println(iter.next());
System.out.println("....................................................");
System.out.println(words.size()+"distinct words."+totalTime+"milliseconds");
}

}

}
...全文
50 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cat_Honey 2008-06-07
  • 打赏
  • 举报
回复
明白啦 ~!是这样两个问题 确实while比for 要好 刚来论坛,分数不多 多谢二位
cat_Honey 2008-06-07
  • 打赏
  • 举报
回复
ah,呵呵 原来这样
不过刚刚修改时候加入 if("bye".equals(word)) break;后又抛出了java.util.NoSuchElementException的异常,为什么呢
becloud 2008-06-07
  • 打赏
  • 举报
回复
import java.util.*;

public class TestSet {

public static void main(String[] args) {

Set<String> words = new HashSet<String>();
long totalTime = 0;

Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String word = in.next();
//要有个标记退出循环,不然就出不去了
if("exit".equalsIgnoreCase(word))
break;
long callTime = System.currentTimeMillis();
words.add(word);
callTime = System.currentTimeMillis() - callTime;
totalTime += callTime;
}
Iterator<String> iter = words.iterator();
//是这样循环Set的,也可以用其他2种方式
while(iter.hasNext()){
System.out.println(iter.next());
System.out.println("....................................................");
}
System.out.println(words.size() + " distinct words. " + totalTime
+ " milliseconds");
}
}
感觉你是这个意思
purerabbit 2008-06-07
  • 打赏
  • 举报
回复
程序一直在while部分运行
当然没有输出结果啊

62,612

社区成员

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

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