求一个VB编程题的解!!!!在线等~

来源:百度知道 编辑:UC知道 时间:2024/06/18 18:19:17
编制函数过程sum,用于计算1个整数的各位数字之和。如引用sum(132)结果施6,引用sum(5),是5,等等。。。
有没有完整的程序???不要一小段

Public Function Sum(ByVal n As Integer)
Do
Sum = Sum + n Mod 10
n = n \ 10
Loop While n <> 0
End Function

Private Sub Command1_Click()
print sum(123)
end sub

被抢了。。。

Public Function Sum(ByVal n As Integer)
Dim s as Integer
s=0
do while n>0
s += n Mod 10
n = n \ 10
Loop While
Sum=s
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
TextBox2.Text = Str(Sum(Val(TextBox1.Text)))
End Sub