调用退出函数时出错递归的

时间:2018-06-22 21:17:38

标签: excel vba

我正在研究递归并创建它。

ABBB运行正常,请确认

BABB运行正常,请确认

BBAB运行正常,请确认

BBBA运行不正常,得到错误

函数的这一部分正常运行,但是当调用退出函数时,如果代码行,则该行跳至最后一端。当我浏览代码时,它会来回跳转三遍。几乎就像是在编译x3的if循环,而不是退出函数。

确定可以。谢谢大家。

Function practieRecursive(userstring, UserStringIndex) As Boolean
    UserStringIndex = UserStringIndex + 1
    If CInt(UserStringIndex) > Len(userstring) Then
        practieRecursive = False
        Exit Function
    ElseIf Mid(userstring, UserStringIndex, 1) = "A" Then
        practieRecursive = True
        Debug.Print practieRecursive
        Exit Function
    Else
        practieRecursive = practieRecursive(userstring, UserStringIndex)
        Exit Function
    End If
    Debug.Print practieRecursive
End Function

1 个答案:

答案 0 :(得分:3)

您正在尝试Mid(userstring, UserStringIndex, 1) = "A",然后再检查CInt(UserStringIndex) = Len(userstring)。重新排列支票。

Function practieRecursive(userstring, UserStringIndex) As Boolean
    UserStringIndex = UserStringIndex + 1
    If CInt(UserStringIndex) = Len(userstring) Then
        practieRecursive = False
        Exit Function
    elseIf Mid(userstring, UserStringIndex, 1) = "A" Then
Stop
        practieRecursive = True
        Exit Function
    Else
        Call practieRecursive(userstring, UserStringIndex)
    End If
End Function