【发布时间】:2023-03-28 10:33:01
【问题描述】:
VBA 掷骰子即使对我来说也很容易,但我需要特定的类型,当掷 6 表示你可以再次掷骰并将两个掷骰相加(加上当你连续掷 6 次时,你会得到第三掷和等等)。
我尝试了两种方法,但都失败了
Sub roll_dice_1()
Dim result As Range
Set result = Range("A1")
result = Application.WorksheetFunction.RandBetween(1, 6)
If result = 6 Then
result = result + Application.WorksheetFunction.RandBetween(1, 6)
Do Until Application.WorksheetFunction.RandBetween(1, 6) <> 6
result = result + Application.WorksheetFunction.RandBetween(1, 6)
Loop
Else
End If
End Sub
然而,这个可以产生 12 的结果,这显然是不可能的,因为两次 6 应该给出第三次滚动
Sub roll_dice_2()
Dim result As Range
Set result = Range("A1")
result = Application.WorksheetFunction.RandBetween(1, 6)
If result = 6 Then
Do Until Application.WorksheetFunction.RandBetween(1, 6) <> 6
result = result + Application.WorksheetFunction.RandBetween(1, 6)
Loop
Else
End If
End Sub
这个更糟糕,因为它可以返回 6。
我尝试搜索高低搜索,但我得到的只是简单的代码,用于简单的掷骰、两个骰子的掷骰以及某些结果可以重新掷骰时的掷骰。同样,所有选项都很简单,不像这个
【问题讨论】:
标签: vba excel loops random dice