【问题标题】:VBA Loop Until ignoring second conditionVBA循环直到忽略第二个条件
【发布时间】:2021-04-12 20:05:28
【问题描述】:

我的RootCheck2 旨在检查它实际上是根还是垂直渐近线。似乎忽略了第二个条件,因为代码当前正在输出渐近线和根。代码输出的Oldx 给出的fOldx 大于5,这让我相信它忽略了第二个条件。如有遗漏请告知,谢谢

Public Function SearchPsi(ByVal Variables As Range, ByVal x As Double) As Double
    Dim Oldx As Double
    Dim Checkx As Double
    Dim fx As Double
    Dim fOldx As Double
    Dim fCheckx As Double
    Dim RootCheck1 As Boolean
    Dim RootCheck2 As Boolean
    
    Do
        Oldx = x
        x = Oldx + 0.1
        Checkx = Oldx + 0.01
        fx = PSI(Variables, x)
        fOldx = PSI(Variables, Oldx)
        fCheckx = PSI(Variables, Checkx)
        
        If (fx * fOldx) < 0 Then
            RootCheck1 = True
        End If
        
        If Abs(fOldx) < 5 Then
            RootCheck2 = True
        End If
    Loop Until RootCheck1 = True And RootCheck2 = True
    
    SearchPsi = Oldx
End Function

【问题讨论】:

  • 一旦RootCheck1RootCheck2 变为True,它们将永远不会变为False,因为当你的条件不成立时,你永远不会将它们设置为False

标签: excel vba loops conditional-statements


【解决方案1】:
Do
    Oldx = x
    x = Oldx + 0.1
    Checkx = Oldx + 0.01
    fx = PSI(Variables, x)
    fOldx = PSI(Variables, Oldx)
    fCheckx = PSI(Variables, Checkx)

Loop Until (fx * fOldx) < 0 And Abs(fOldx) < 5 

【讨论】:

    猜你喜欢
    • 2017-08-04
    • 2018-08-13
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2023-04-09
    • 2021-07-05
    相关资源
    最近更新 更多