【问题标题】:while loop breaks before it's supposed to, logic issue?while 循环在它应该中断之前中断,逻辑问题?
【发布时间】: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 哦,我明白了,很抱歉,它没有

标签: java loops logic


【解决方案1】:

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

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

(我认为应该)

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

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

【讨论】:

  • 呵呵,是的,我在您发布此内容的同时就明白了,谢谢!!!!
  • 即使它有效,这也不是遍历列表的好方法。只有在事先不知道迭代次数的情况下才应该使用 while 循环。
  • @Dici 由于 Java 编译器和 JIT 将优化性能上的任何差异,为什么您建议不要使用 while?
  • @dici 我不知道确切的数字,用户输入自己的字典,所以它可以是任意大小
  • 字典在循环过程中不会改变大小,所以你肯定知道迭代次数。
【解决方案2】:

其实我只是想通了!!

问题是“计数器”位于字典大小的末尾,我需要做的就是添加一个东西,在每次遍历字典后将计数器归零!哦。我应该删除这个问题吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2020-08-05
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    相关资源
    最近更新 更多