需要骰子投掷组合的帮助

我正在做一个VBA程序的程序是这样的:

你知道你用两个骰子滚动的数字(滚动的数字是由用户填写),那么程序需要显示每个可能的组合

所以2应该是1和1的3应该是1和2,但也2和1等等。

截至目前,我正在尝试用随机数字来做,但是当我确定x值可能不等于我的x1值时,程序会冻结。

有人会帮我这么好吗? :d

提前致谢。

我的程序到目前为止:

Sub Button1_Click() Dim invoer As Integer invoer = Range("A1") If invoer < "2" Then MsgBox ("kan niet gegooid worden") End If If invoer > "12" Then MsgBox ("kan niet gegooid worden") End If Do x = Int((6 * Rnd) + 1) y = Int((6 * Rnd) + 1) Loop Until x + y = invoer If x + y = invoer Then Range("a3") = x Range("b3") = y Do x1 = Int((6 * Rnd) + 1) y1 = Int((6 * Rnd) + 1) Loop Until (x1 = Not x) + (y1 = Not y) = invoer If x1 = Not x Then Range("a4") = x1 Range("b4") = y1 End If End If End Sub 

你不需要复杂的RND()的概念,这是非常简单的math。 你需要在正常的两个骰子上从2(最小)到12(最大)循环。 每增加一个,你就从另一个死亡拿走。

我真诚地认为,这是所有需要的。 祝你好运。

 Sub Button1_Click() Dim inVoer As Integer, dCtr As Integer Dim rCtr As Integer 'Reads the Number from the Cell inVoer = Range("A1") 'Checks if the number entered is valid for a two die combination. 'The minimum you can get on a two die is 2, and the maximum is 12. If inVoer <= 1 Or inVoer > 12 Then 'If anything not between 2 and 12, a message is displayed for the user. MsgBox ("kan niet gegooid worden") Exit Sub End If 'This counter is used to get the next ROW to print the result to. rCtr = 2 'Loop from the minimum, to the number entered in the CELL. For dCtr = 2 To inVoer 'As each die can have only 1 to 6, we need to make sure the numbers generated, 'are between 1 and 6, anything greater is not desired. If (dCtr - 1 <= 6) And (inVoer - dCtr + 1) <= 6 Then 'The Logic - If die 1 has 'x', die 2 'y'. x + y should give inVoer. 'So -> dCtr - 1 + inVoer - dCtr + 1 = inVoer 'dCtr - 1 will give the result of the first die. Range("A" & rCtr + 1) = dCtr - 1 'inVoer - dCtr - 1, gives the result of the other die. Range("B" & rCtr + 1) = inVoer - dCtr + 1 'Increment the ROW Counter rCtr = rCtr + 1 End If Next End Sub 
 Dim inVoer As Integer, dCtr As Integer, cCtr As Integer For dCtr = 2 To inVoer 'As each die can have only 1 to 6, we need to make sure the numbers generated, 'are between 1 and 6, anything greater is not desired. If (dCtr - 1 <= 6) And (inVoer - dCtr + 1) <= 6 Then 'The Logic - If die 1 has 'x', die 2 'y'. x + y should give inVoer. 'So -> dCtr - 1 + inVoer - dCtr + 1 = inVoer 'dCtr - 1 will give the result of the first die. Range("A" & rCtr + 1) = dCtr - 1 'inVoer - dCtr - 1, gives the result of the other die. Range("B" & rCtr + 1) = inVoer - dCtr + 1 'Increment the ROW Counter rCtr = rCtr + 1 End If 'trying a new next with dCtr 3 to inVoer, so new formula x + y + z? For cCtr = 3 To inVoer If (cCtr - 2 <= 6) And (dCtr - 1 <= 6) And (inVoer - (cCtr + 2 - dCtr + 1 / 2)) <= 6 Then If ((cCtr - 2 <= 6) And (dCtr - 1 <= 6)) = inVoer Then Exit For Range("A" & rCtr + 1) = dCtr - 1 Range("B" & rCtr + 1) = cCtr - 2 'this can give a negative number Range("C" & rCtr + 1) = inVoer - cCtr + 2 - dCtr + 1 rCtr = rCtr + 1 End If Next Next 

所以我设置一个新的cCtr为整数给自己一种方式来计算,这样invoer是我的输出。

在这一点上,你可以看到问题是我的范围C中的inVoer变成负值。 如果有人可以帮我解决这个问题。 我可以完成它! 🙂