我已经实现了一个执行以下操作的程序:
拼写检查程序将dictionary.txt文件加载到数组中,并将字符串输入与字典内的单词进行比较.
我当前的问题是,当输入包含多次相同的单词时,例如"teh program was teh worst",代码将打印出来
You entered 'teh', did you mean 'the'?
You entered 'teh', did you mean 'the'?
Run Code Online (Sandbox Code Playgroud)
有时网站会反复出现多个单词,这可能会变得混乱.
如果可能的话,打印单词以及错误拼写的次数将是完美的,但是对每个打印一次的单词施加限制就足够了.
我的程序有一些方法和两个类,但拼写检查方法如下:
注意:原始代码包含一些删除标点符号的"if"语句,但为了清楚起见,我删除了它们.
static boolean suggestWord;
public static String checkWord(String wordToCheck) {
String wordCheck;
String word = wordToCheck.toLowerCase();
if ((wordCheck = (String) dictionary.get(word)) != null) {
suggestWord = false; // no need to ask for suggestion for a correct
// word.
return wordCheck;
}
// If after all of these checks a word could not …Run Code Online (Sandbox Code Playgroud)