首页 > 代码库 > 求前n项的和【VB代码实现】

求前n项的和【VB代码实现】

技术分享的和。

For语句实现

Private Sub Command1_Click()
    Dim s As Long
    n = Val(Text1.Text)
    s = 0
    For i = 1 To n
        s = s + i
    Next i
    Label4.Caption = Str(s)
End Sub

 Do While语句实现

Private Sub Command1_Click()
    Dim s As Long, i As Integer, n As Integer
    n = Val(Text1.Text)
    s = 0
    i = 1
    Do While i <= n
        s = s + i
        i = i + 1
    Loop
    Label4.Caption = Str(s)
End Sub

  

求前n项的和【VB代码实现】