Excel中,在vba中计算几个循环,输出结果
要计算一个循环,n=1,m=1,取两个随机数a=randbetween(1,30),b=randbetween(1,100),如果a>b,则n=n+1,否则n=n-1,n...
要计算一个循环,n=1,m=1,取两个随机数a=randbetween(1,30),b=randbetween(1,100),如果a>b,则n=n+1,否则n=n-1,n如果等于1时,则不减,m表示计算的次数,每计算一次,m=m+1,最后输出m=?时n=5.输出大量个m,计算m的平均数。
求Excel大神指导下。 展开
求Excel大神指导下。 展开
4个回答
展开全部
Sub xx()
For i = 1 To 10
m = 0
n = 1
Do Until n = 5
a = Application.WorksheetFunction.randbetween(1, 30)
b = Application.WorksheetFunction.randbetween(1, 100)
If a > b Then
n = n + 1
Else
n = Application.WorksheetFunction.Max(1, n - 1)
End If
m = m + 1
Loop
avm = (avm * (i - 1) + m) / i
Next i
MsgBox avm
End Sub
For i = 1 To 10
m = 0
n = 1
Do Until n = 5
a = Application.WorksheetFunction.randbetween(1, 30)
b = Application.WorksheetFunction.randbetween(1, 100)
If a > b Then
n = n + 1
Else
n = Application.WorksheetFunction.Max(1, n - 1)
End If
m = m + 1
Loop
avm = (avm * (i - 1) + m) / i
Next i
MsgBox avm
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码如下
Sub asdf()
[a1:d1].ClearContents
n = 1
m = 0
pj = 0
ii = 0
Do Until n = 5
Randomize
a = Int(30 * Rnd) + 1
Randomize
b = Int(100 * Rnd) + 1
If a > b Then
n = n + 1
ElseIf a < b And n <> 1 Then
n = n - 1
End If
m = m + 1
pj = pj + m
ii = ii + 1
Loop
[a1] = "n值为:" & n
[b1] = "m值为:" & m
[c1] = "所有m值的总和为:" & pj
[d1] = "m的平均数为:" & pj / ii
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Public Sub aa()
Dim m As Integer
Dim n As Integer
m = 0
n = 1
Dim a As Integer
Dim b As Integer
Do Until n = 5
a = Application.RandBetween(1, 30)
b = Application.RandBetween(1, 30)
If a > b Then
n = n + 1
Else
If n <> 1 Then
n = n - 1
End If
End If
m = m + 1
Loop
Cells(1, 1) = n
Cells(1, 2) = m
End Sub
以上是VBA代码,用于sheet
Dim m As Integer
Dim n As Integer
m = 0
n = 1
Dim a As Integer
Dim b As Integer
Do Until n = 5
a = Application.RandBetween(1, 30)
b = Application.RandBetween(1, 30)
If a > b Then
n = n + 1
Else
If n <> 1 Then
n = n - 1
End If
End If
m = m + 1
Loop
Cells(1, 1) = n
Cells(1, 2) = m
End Sub
以上是VBA代码,用于sheet
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如下代码可能会是一个死循环,
sub sss()
n=1
m=1
do while n<>5
a=randbetween(1,30)
b=randbetween(1,100)
if a>b then
n=n+1
else
n=n-1
end if
m=m+1
loop
msgbox m
end sub
sub sss()
n=1
m=1
do while n<>5
a=randbetween(1,30)
b=randbetween(1,100)
if a>b then
n=n+1
else
n=n-1
end if
m=m+1
loop
msgbox m
end sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询