我正在写一个拼写检查程序,我必须写的一个函数必须找到拼写错误的单词的建议。因此,我使用递归来遍历我的基数树并找到所有的建议。但是,当递归执行时,我使用的其中一个计数器不断增加,然后减小,然后再增加,实际上它应该增加。这是函数的代码。如何在使用递归时保持一致性?
public void findSuggest(Edge tmp, String suffix, int app)
{
int used = 0;
if(tmp.tNode.children > 10)
{
used = 10;
}
else
{
used = tmp.tNode.children;
}
for(int j = 0; j < used; j++){
if(app <= 9)
{
if((!"#".equals(tmp.prefix))&&(!"#".equals(tmp.tNode.edges[j].prefix))){
suggest[app].append(tmp.prefix);
suggest[app].append(tmp.tNode.edges[j].prefix);
System.out.println("tmp.prefix: " + tmp.prefix);
System.out.println("tmp.prefix............: " + tmp.tNode.edges[j].prefix);
app++;
if(tmp.tNode.edges[j].tNode != null)
{
suggest[app].append(tmp.prefix);
System.out.println("App: " + app);
findSuggest(tmp.tNode.edges[j], suffix, app++);
}
}
}
}
}
,这是我得到的输出: 应用程序是计数器,tmp.prefix是父节点的前缀和tmp.prefix .......是孩子的前缀。
应用程序:0
tmp.prefix:吨
tmp.prefix ............:电子
应用程序:1
tmp.prefix:e
tmp.prefix ............:s
应用程序:2
tmp.prefix:吨
tmp.prefix ............:ⅰ
应用程序:3
tmp.prefix:ⅰ
tmp.prefix ............:C
应用程序:4
tmp.prefix:C
tmp.prefix ............:人
应用程序:5
tmp.prefix:ⅰ
tmp.prefix ...... ......:SE
应用程序:6
tmp.prefix:SE
tmp.prefix ............:d
应用程序:7
tmp.prefix:SE
tmp.prefix ............:■
应用程序:7
tmp.prefix :ⅰ
tmp.prefix ............:泽
应用程序:8
tmp。前缀:泽
tmp.prefix ............:d
应用:9
tmp.prefix:泽
tmp.prefix ... .........:■
应用程序:4
tmp.prefix:吨
tmp.prefix ............:URE
应用程序:0
tmp.prefix:X
tmp.prefix ............:电子
应用程序:1个
tmp中。前缀:电子
tmp.prefix ............:d
应用程序:2
tmp.prefix:电子
tmp.prefix ............:■
应用程序:2
tmp.prefix:X
TMP .PREFIX ............:荷兰国际集团
(这是所有单词建议内置字符串数组的最终结果)
climatexe
climatesxed
climatiesxing
气候
climaicalture
climaise
climaised
climasesize
climaized
climazes
许多答案都没有帮助。 – Chris