gpt4 book ai didi

java - while 循环提前中断,逻辑问题?

转载 作者:行者123 更新时间:2023-11-29 05:16:19 26 4
gpt4 key购买 nike

在我正在编写的程序中,我有一个单词列表,对于每个单词,此方法应该遍历整个字典并将给定单词与字典进行比较。出于某种原因,它只比较第一个单词,但在不遍历字典的情况下继续通过 givenWordCounter 递增。

这一定是我的逻辑有问题,但我不知道它是什么。下面是我想出的伪代码以及代码本身,你看到我犯了什么大错误吗?

伪代码:

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);
}
}



}

最佳答案

交换(并稍微改变)这两行,

int thouCount = 1000;
while (givenWordCounter < givenWords.size()){

(我认为应该)

while (givenWordCounter < givenWords.size()){
int thouCount = 1000 + givenWordCounter; // <-- to offset givenWordCounter.

因为您需要在每次循环迭代时重置 thouCount

关于java - while 循环提前中断,逻辑问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26452484/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com