VB的数据算法是有大小的,你整308个9当然要出错了.
大数位计算一般都是通过字符串来实现的。你可以研究一下这个例子,大数阶乘的计算
复制内容到剪贴板
代码:
Private Sub Command1_Click()
MsgBox Factorial(10)
End Sub
Public Function Factorial(intC As Integer) As String
On Error GoTo myErr:
Dim intA() As Integer, intT() As Integer, i As Integer
Dim j As Integer, intR As Integer
ReDim intA(20), intT(20)
If intC < 2 Then Exit Function
intR = 20
intA(0) = 1
For i = 2 To intC
For j = 0 To intR
intT(j) = intA(j) * i
Next j
j = 0
Do Until j > intR
intA(j) = intT(j) Mod 10
If intT(j) >= 10 Then intT(j + 1) = intT(j + 1) + intT(j) \ 10
j = j + 1
Loop
Next i
Do While True
j = j - 1
If intA(j) > 0 Then Exit Do
Loop
For j = j To 0 Step -1
Factorial = Factorial & intA(j)
Next j
Exit Function
myErr:
If Err.Number = 9 Then
intR = intR + 20
ReDim Preserve intA(intR)
ReDim Preserve intT(intR)
Resume
Else
MsgBox "UnKnow ERR:" & Err.Description & ":" & Err.Number, vbCritical + vbOKOnly
End If
End Function