
while(in.hasNext())用户输入一行字符串后回车,无法结束输入状态 10
这个程序是计算查找相似度的,先由用户输入一个文件名,,然后再由用户输入一行的关键字,比如“cowslikerednike”输入完成后按回车就应该得到计算结果的,但是下面的...
这个程序是计算查找相似度的,先由用户输入一个文件名,,然后再由用户输入一行的关键字,比如“cows like red nike”输入完成后按回车就应该得到计算结果的,但是下面的问题是,按回车后,console依然是等待用户输入状态。问题应该出现在while(input.hasNext()){ 这个循环里了:
package query;
import java.io.*;
import java.util.*;
public class Query {
public static void main(String[] args)throws FileNotFoundException{
System.out.println("please enter a document u want to query: ");
Scanner in = new Scanner(System.in);
String filename = in.next();
Set<String> words = new HashSet<String>();
Scanner input = new Scanner(new File(filename));
input.useDelimiter("[^a-zA-Z]+");
while(input.hasNext()){
words.add(input.next().toLowerCase());
}
System.out.println("unique words is "+words.size());
System.out.println("please enter a query: ");
Set<String> keywords = new HashSet<String>();
while(in.hasNext()){
String s1 = in.next();
System.out.println(s1);
if(s1.equals("exit")) {
break;
}
keywords.add(in.next().toLowerCase());
}
int qIntersectD=0;
for(String word :keywords){
if(words.contains(word)){
qIntersectD++;
}
}
System.out.println(qIntersectD);
double Similarity=0;
Similarity=qIntersectD/(Math.sqrt(words.size())*Math.sqrt(keywords.size()) );
System.out.println("the similarity between query and document is : "+Similarity);
System.out.println("unique words is "+words.size());
}
} 展开
package query;
import java.io.*;
import java.util.*;
public class Query {
public static void main(String[] args)throws FileNotFoundException{
System.out.println("please enter a document u want to query: ");
Scanner in = new Scanner(System.in);
String filename = in.next();
Set<String> words = new HashSet<String>();
Scanner input = new Scanner(new File(filename));
input.useDelimiter("[^a-zA-Z]+");
while(input.hasNext()){
words.add(input.next().toLowerCase());
}
System.out.println("unique words is "+words.size());
System.out.println("please enter a query: ");
Set<String> keywords = new HashSet<String>();
while(in.hasNext()){
String s1 = in.next();
System.out.println(s1);
if(s1.equals("exit")) {
break;
}
keywords.add(in.next().toLowerCase());
}
int qIntersectD=0;
for(String word :keywords){
if(words.contains(word)){
qIntersectD++;
}
}
System.out.println(qIntersectD);
double Similarity=0;
Similarity=qIntersectD/(Math.sqrt(words.size())*Math.sqrt(keywords.size()) );
System.out.println("the similarity between query and document is : "+Similarity);
System.out.println("unique words is "+words.size());
}
} 展开
5个回答
展开全部
Java API 中的解释
public boolean hasNext()
Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.
如果没有输入的话,会被阻塞而不是退出。
至于怎么刷题……至少不应该用hasNext()与hasNextLine()来判断停止。所以一般题目一开始就输入会有多少行等等。要是笔试题(那种卷子类的)没有的话,直接写LeetCode类的函数吧,别管输入输出了哈哈哈。
展开全部
调用完方法后要在while (in.hasNext() ){ 调用方法 之后放有break;}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
打exit就退出输入了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |