下列VB程序错在哪?

来源:百度知道 编辑:UC知道 时间:2024/06/20 09:16:33
我刚自学VB,是个超级菜鸟
以下是我编的双色球红球和值程序,要求输入和值t,程序则把所有的组合都排出来。程序错在哪?怎么修改?
Private Sub command1_Click()
Dim t As Integer
t = Text1
For a = 1 To 33
For b = 1 To 33
For c = 1 To 33
For d = 1 To 33
For e = 1 To 33
For f = 1 To 33
If a + b + c + d + e + f = t And a <> b <> c <> d <> e <> f Then
Print a, b, c, d, e, f

End If
Next a
Next b
Next c
Next d
Next e
Next f
End Sub

Private Sub Command2_Click()
End
End Sub

你那样循环的话,会重复,导致多循环很多次

另外t不能那样赋值,text1是文本,直接赋值给integer会出错,要转换的

改成下面的:

Private Sub command1_Click()
Dim t As Integer
t = val(Text1)
For a = 1 To 28
For b =a + 1 To 29
For c =b + 1 To 30
For d =c + 1 To 31
For e =d + 1 To 32
For f =e + 1 To 33
If a + b + c + d + e + f = t Then Print a, b, c, d, e, f
Next f
Next e
Next d
Next c
Next b
Next a
End Sub

我以前编过双色球按条件排列,我这样循环要快些

循环错误,你的逻辑有问题。
把next a 到 next f 弄反了。
我知道你的想法是把这种情况全部例举出来
但是情况太多,VB会崩的

Private Sub command1_Click()
Dim t As Integer
t = Text1.text
For a = 1 To 33
For b = 1 To 33
For c = 1 To 33
For d = 1 To 33
For e = 1 To 33
For f = 1 To 33
If a + b + c + d + e + f = t And a <> b <> c <> d <> e <> f Then
Print a, b, c, d, e, f

End If
Next f
Next e
Next d
Next c
Next