2017年12月13日 星期三

[ VB2010 ] -用遞迴求陣列元素總和

[ VB2010 ] -用遞迴求陣列元素總和



Public Class Form1

    Public DATA(100) As Integer
    Public num1, I As Integer

    Private Function rsum(ByVal DATA() As Integer, ByVal num1 As Integer) '{ /*遞迴地求算陣列元素總和*/
        If (num1 < 1) Then
            Return 0
        Else
            Return (rsum(DATA, num1 - 1) + DATA(num1))  '/*遞迴地求剩下解*/
        End If

    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox4.Text = ""
        num1 = Val(TextBox2.Text)
        TextBox4.Text &= CStr(rsum(DATA, (num1)) + DATA(0))

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        I = 0

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim K As Integer
        num1 = Val(TextBox2.Text)
        If I <= (num1 - 1) Then
            DATA(I) = Val(TextBox3.Text)
            Label4.Text = "[" & (I).ToString & "]"
        Else
            TextBox3.Text = "已完成輸入"
            For K = 0 To num1 - 1
                TextBox5.Text &= DATA(K) & ","
            Next

        End If
        I += 1

    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim k As Integer
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox3.Text = ""
        TextBox2.Text = ""
        I = 0
        For k = 0 To 99
            DATA(I) = 0
        Next

    End Sub
End Class

沒有留言:

張貼留言

2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2

 2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2 AngularJS 實例 <!DOCTYPE html> <html> <head> &...