【发布时间】:2014-12-14 15:47:56
【问题描述】:
在我正在编写的程序中,我有一个单词列表,对于每个单词,此方法应该遍历整个字典并将给定单词与字典进行比较。出于某种原因,它只比较第一个单词,但继续通过给定的WordCounter 递增而不通过字典运行它。
这一定是我的逻辑有问题,但我不知道是什么。下面是我想出的伪代码以及代码本身,你看到我犯了什么大错误吗?
伪代码:
for each word in the givenWords list:
while dictionaryCounter < dictionary.size():
run word in givenWord list at givenWordCounter index through the dictionary
increment thousandCounter by 1000
increment givenWordCounter by 1
program should loop through every word in givenWords list & for each givenWord
loop through dictionary.prefix once. after dictionary.prefix loop is done move
on to next givenWord
和代码:
public static void runProgram(){
int givenWordCounter = 0;
int thouCount = 1000;
while (givenWordCounter < givenWords.size()){
while (thouCount < theDictionary.size()){
Dictionary.prefix(givenWords.get(givenWordCounter), theDictionary, counter, thouCount);
thouCount = thouCount + 1000;
}
givenWordCounter++;
}
}
根据我对如何编写代码的理解,它应该可以工作,但我想我犯了一个大错误,我猜?
编辑: 我被要求包含在 Dictionary 类中调用的代码,这里是(需要通过赋值递归):
public static void prefix (String origWord, List<String> theDictionary, int beginFrom, int endAt){
// if the words don't match recurse through this same method in order to move on to the next word
if (beginFrom < theDictionary.size() && counter < endAt){
if ( origWord.charAt(0) != theDictionary.get(beginFrom).charAt(0) || origWord.length() != theDictionary.get(beginFrom).length()){
counter = counter + 1;
prefix(origWord, theDictionary, beginFrom+1, endAt);
}
// if the words first letter and size match, send the word to prefixLetterChecker to check for the rest of the prefix.
else{
prefixLetterChecker(origWord, theDictionary.get(beginFrom), 1);
counter = counter + 1;
prefix(origWord, theDictionary, beginFrom+1, endAt);
}
}
}
【问题讨论】:
-
你能给我们一些
Dictionary类的代码吗?这是Iterable吗?你对 while 循环的使用不好,for 或 foreach 循环在这里会好一百倍 -
确保编辑包含此代码调用的 Dictionary 类中的前缀方法
-
@dici 好的,我更新了它
-
谢谢,但您添加的代码与此处无关。我只是想知道它是否正在实现
Iterable接口 -
@dici 哦,我明白了,很抱歉,它没有