求Sn=a+aa+aaa+……+aaa……a(n个a)的值,例如当a=2,n=4 时Sn=2+22+222+2222,a 和n 由键盘输出
这题应该怎么写,我一直想不好

Private Function SUMa(ByVal A As Long, ByVal N As Long) As Long
Dim I As Long, TempNum As Long
On Error GoTo SUMaError '发生错误跳转到SUMaError
If A < 1 Or N < 1 Or A > 10 Then GoTo SUMaError '参数错误则跳转
For I = 1 To N
TempNum = TempNum + Val(String(I, CStr(A)))
Next
SUMa = TempNum
Exit Function
SUMaError:
SUMa = -1 '发生错误时返回-1(调用参数错或是溢出)
End Function
[此贴子已经被作者于2007-5-22 22:00:00编辑过]
Option Explicit
Private Sub Command1_Click()
Dim Sn As Long, aa As Long
Dim n As Integer, a As Integer
Dim i As Integer
a = Val(Text1.Text)
n = Val(Text2.Text)
For i = 1 To n
aa = aa* 10 + a
Sn = Sn + aa
Next
Label1.Caption = Sn
End Sub
aa 没初值,是不是就默认为0?
Private Function SUMa(ByVal A As Long, ByVal N As Long) As Long
Dim I As Long, TempNum As Long
On Error GoTo SUMaError '发生错误跳转到SUMaError
If A < 1 Or N < 1 Or A > 10 Then GoTo SUMaError '参数错误则跳转
For I = 1 To N
TempNum = TempNum + Val(String(I, CStr(A)))
Next
SUMa = TempNum
Exit Function
SUMaError:
SUMa = -1 '发生错误时返回-1(调用参数错或是溢出)
End Function
String(I, CStr(A))這個函數這是一種什麼用法,什麼意思,好像是進行A的I次連接,但我沒見過這種用法,只知道它是轉化為字符串,能請樓主介紹一下這種用法嗎
long够用不?
string的上限是255
语法
String(number, character)
String 函数的语法有下面的命名参数:
部分 说明
number 必要参数;Long。返回的字符串长度。如果 number 包含 Null,将返回 Null。
character 必要参数;Variant。为指定字符的字符码或字符串表达式,其第一个字符将用于建立返回的字符串。如果 character 包含 Null,就会返回 Null。
说明
如果指定 character 的数值大于 255,String 会按下面的公式将其转为有效的字符码:
character Mod 256