gpt4 book ai didi

arrays - 将随机骰子的值放入数组(VBA)

转载 作者:行者123 更新时间:2023-12-04 21:56:33 25 4
gpt4 key购买 nike

我试图弄清楚如何创建三个给定函数返回值的数组。我没有将数据放入任何地方的excel,所以我不能使用“范围”。我相信有一个解决方案,但是我还没有弄清楚。

'randomize first dice
Function DiceOne(ByRef intDiceOne As Integer)
intDiceOne = Application.WorksheetFunction.RandBetween(1, 6)
DiceOne = intDiceOne
End Function
'randomize second dice
Function DiceTwo(ByRef intDiceTwo As Integer)
intDiceTwo = Application.WorksheetFunction.RandBetween(1, 6)
DiceTwo = intDiceTwo
End Function
'add first and second dice
Function RollDice()
Dim intDiceOne As Integer
Dim intDiceTwo As Integer
Dim intSumDice As Integer
Dim i As Integer
DiceOne intDiceOne
DiceTwo intDiceTwo
intSumDice = intDiceOne + intDiceTwo
RollDice = intSumDice
'Application.Range("dice_one") = intDiceOne
'Application.Range("dice_two") = intDiceTwo
'Application.Range("dice_sum") = intSumDice

'Debug.Print "The roll value is " & intSumDice
End Function

最佳答案

您不需要为每个 die 使用单独的函数,也不需要 ByRef .做这个:

Function RollD6()
RollD6 = Application.WorksheetFunction.RandBetween(1, 6)
End Function


Sub RollDice()
Dim Dice(2) As Integer
For i = 0 To 2
Dice(i) = RollD6()
Next
Debug.Print("Dice: " & Dice(0) & " " & Dice(1) & " " & Dice(2))
Debug.Print("Sum: " & Dice(0) + Dice(1) + Dice(2))
End Sub

关于arrays - 将随机骰子的值放入数组(VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43566794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com