我正在开发一个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
答案 0 :(得分:1)
你不需要复杂的RND()概念,这是非常简单的数学。你需要在正常的两个骰子上从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
答案 1 :(得分:0)
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变为负值。如果有人可以帮我解决这个问题。我可以完成它! :)