excel中vba的代码,输出结果跟想的不一样,求哪位大神给看下~

2024-11-07 12:36:11
推荐回答(1个)
回答(1):

你的 年=0,m=0方错地方了

Sub js()
Dim aver As Double, sum As Double, a As Integer
Dim arrA(100000) As Double '定义数组记录每一次升到10级所需要的次数
sum = 0 '前i次升级到10级一共的次数
S = 10 '要求到达10级
For i = 1 To 100000 '计算10000次
n = 0: m = 0
Do While n <> S
Randomize
a = Int(100 * Rnd + 1)
If a >= 30 Then
n = n + 1: m = m + 1
Else
If n > 1 Then
n = n - 1: m = m + 1
Else
m = m + 1
End If
End If
Loop
Sheet1.Cells(i, 1) = m
arrA(i) = m '将当前升级所需次数赋值到数组中
sum = sum + arrA(i) '计算前n次的和
Next
aver = sum / 100000 '求平均数
Sheet1.Cells(1, 2) = aver
End Sub