Cry*_*ork 6 excel vba excel-vba
所以这个问题比简单的比较更深入.基本上我试图模拟这个骰子卷,称为滚动和保持系统.例子是5k3.在哪里我会掷5个骰子并保持3个最高,然后将它们加在一起.
我已经得到了我的小宏程序来掷骰子.然后我将它们放在我的示例中的数组中,该数组将是一个包含5个索引的数组.现在我需要拿这5个骰子,并且只保留其中最大的3个.
代码在这里A2给了我骰子的数量,B2给了我多少我滚动,C2给了我多少我保持.这会掷10个骰子,但随后我将其中的5个转移到我的实际骰子中.我知道我可以跳过这个,但我可以稍后处理.
Private Sub CommandButton1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim RandNum As Integer
Dim RollArray() As Integer
Dim KeptArray() As Integer
Dim RollArrayDummy() As Integer
Dim NumRoll As Integer
Dim Kept As Integer
Dim Largest As Integer
NumRoll = Range("B2").Value
ReDim RollArray(NumRoll)
Kept = Range("C2").Value
ReDim KeptArray(Kept)
For i = 5 To 15
Randomize
RandNum = 1 + Rnd() * (Range("A2").Value - 1)
Cells(i, 1).Value = RandNum
Next i
For j = 1 To NumRoll
RollArray(j) = Cells(4 + j, 1).Value
Cells(4 + j, 2).Value = RollArray(j)
Next j
k = 1
i = 1
m = 1
Largest = 1
For k = 1 To Kept
m = 1
KeptArray(k) = Largest
If m <= NumRoll Then
If Largest >= RollArray(m) And Largest >= KeptArray(k) Then
Largest = KeptArray(k)
Else
KeptArray(k) = Largest
Largest = RollArray(m)
End If
m = m + 1
End If
Cells(4 + k, 3).Value = KeptArray(k)
Next k
End Sub
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多东西,比如创建一个虚拟数组,并将变量Largest与它进行比较.还有很多其他的东西.我的大问题是我不能重复使用任何数字.
如果我滚动5并保持3.说我滚[4,2,3,3,6].我保留[6,4,3].我敢肯定这是非常简单,我忽略它,但它驱使我绝对疯了.
试试这个,改变了一些东西:也编辑了随机位
Private Sub CommandButton1_Click()
Dim i As Long, j As Long, k As Long
Dim RandNum As Long
Dim RollArray() As Long
Dim KeptArray() As Long
Dim NumRoll As Long
Dim Kept As Long
NumRoll = Range("B2").Value
ReDim RollArray(1 To NumRoll)
Kept = Range("C2").Value
ReDim KeptArray(1 To Kept)
For i = 5 To 15
Randomize
'RandNum = 1 + Rnd() * (Range("A2").Value - 1)
RandNum = 1 + Int(Rnd() * Range("A2").Value)
Cells(i, 1).Value = RandNum
Next i
For j = 1 To NumRoll
RollArray(j) = Cells(4 + j, 1).Value
Cells(4 + j, 2).Value = RollArray(j)
Next j
For k = 1 To Kept
KeptArray(k) = Application.WorksheetFunction.Large(RollArray, k)
Cells(4 + k, 3).Value = KeptArray(k)
Next k
End Sub
Run Code Online (Sandbox Code Playgroud)
利用Excel大函数
归档时间: |
|
查看次数: |
96 次 |
最近记录: |