[ VB2010 ] -用遞迴 求算1*2+2*3+3*4+…+(n-1)*n之和.
Public Class Form1
Private Function sum(ByVal n As Integer) '/*遞迴程式部分*/
If (n = 1) Then
Return 0
Else
Return sum(n - 1) + n * (n - 1)
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n As Integer
n = Val(TextBox2.Text)
TextBox3.Text = sum(n).ToString()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub
End Class
沒有留言:
張貼留言