用vb,求1+1⼀(1+2)+1⼀(2+3)+1⼀(3+4)+……+1⼀(n+n+1)的和

2025-06-24 08:56:51
推荐回答(1个)
回答1:

Private Sub Command1_Click()
    'n自己填,我这里填的6 
    MsgBox test1(6) & " = " & test(6)
End Sub
 
'你要的算法主要是这个。。。  
Private Function test(ByVal n As Long) As Double
    If n > 0 Then
        test = test(n - 1) + 1 / (n + n + 1)
    Else
        test = 1
    End If
End Function

'显示拼接的公式,可以忽略 
Private Function test1(ByVal n As Long) As String
    If n > 0 Then
        test1 = test1(n - 1) & " +  1 / (" & n & " + " & n + 1 & ")"
    Else
        test1 = "1"
    End If
End Function