public static void main(String[] args)throws Exception {
String name1,name2,enterword;
int a=0,b=0,c=0;
String filename="E:/aaa.txt";
File file=new File(filename);
Scanner in =new Scanner(System.in);
Scanner indata=new Scanner(file);
for(int x=0;x<10;x++) {
System.out.println("please enter your english word");
enterword=in.nextLine();
while(indata.hasNext()) {
name1=indata.next();
name2=indata.next();
if(enterword.equals(name1)) {
System.out.println("In Spanish ,"+name1+" called "+name2);
}
if(enterword.equals("q")||enterword.equals("Q")) {
System.out.println("program has exited");
break;
}
if(!enterword.equals(name1)) {
c++;
}
if(c==5) {
System.out.println("we can't find this word in dictionary");
}
}
}
System.exit(0);
}
} 本人初学java。先简单说下程序目的:要求再输入一个英语单词后自动从文档中找到匹配的单词翻译。单词的翻译需要提前输入到指定的记事本中。输入“q”来终止程序,并且要求每一次执行结束后再次弹出输入框(除非终止程序)。现在的问题是,每当该程序在第二次循环时,都会自动略过代码中的while(indata.hasNext())部分。也就是说无论输入什么,会一直循环弹出输入框。这是什么原因。还是说我不应该使用hasNext来解决这个问题。但目前为止所学的知识只有这么多。问题到底出在哪里了?求大佬解答!
添加回答
举报
0/150
提交
取消