[ VB2010 ] --用遞迴方法 輸入一個整數n,並用遞迴求解1+2+3…n之和.
Public Class Form1
Private Function sum(ByVal n As Integer) '/*遞迴程式部分*/
If n = 1 Then
Return 1
Else
Return sum(n - 1) + n
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

沒有留言:
張貼留言