以下是引用shi781203在2011-8-28 17:39:07的发言:
本人想利用VB编写九宫格、十六宫格、二十五宫格、三十六宫格、四十九宫格、六十四宫格、八十一宫格等程序时遇到非代码难点无法解决请高手指点指点!
其中九宫格数代码如下:
Dim x(1 To 9) As Integer
'代表九宫格的数值
Dim a(1 To 8) As Integer
'代表横向、坚向及斜向相加的数值
Dim TongGuo As Boolean
'当枚举出结果时就认程序停止
Private Sub Command1_Click()
'单击执行扭
TongGuo = False
For s1 = 1 To 9
'分别枚举各宫格中的数值不论是否相同
For s2 = 1 To 9
For s3 = 1 To 9
For s4 = 1 To 9
For s5 = 1 To 9
For s6 = 1 To 9
For s7 = 1 To 9
For s8 = 1 To 9
For s9 = 1 To 9
x(1) = s1
x(2) = s2
x(3) = s3
x(4) = s4
x(5) = s5
x(6) = s6
x(7) = s7
x(8) = s8
x(9) = s9
Bs
'调用过程
If TongGuo = True Then
'通过就显示结果到文本框1组中并跳出多层循环
For i = 0 To 8
Text1(i).Text = x(i + 1)
Next
Exit For
End If
Next
Next
Next
Next
Next
Next
Next
Next
Next
End Sub
Private Sub Bs()
'该过程用于排除九个格有相同数值的结果
For i = 1 To 8
For j = i + 1 To 9
If x(i) <> x(j) Then
TongGuo = True
Else
TongGuo = False
Exit For
End If
Next
Next
If TongGuo = True Then
'把找到的结果临时显示到文本框2组中
For i = 0 To 8
Text2(i).Text = x(i + 1)
Next
a(1) = x(1) + x(2) + x(3)
'把找到的结果进行计算
a(2) = x(4) + x(5) + x(6)
a(3) = x(7) + x(8) + x(9)
a(4) = x(1) + x(4) + x(7)
a(5) = x(2) + x(5) + x(8)
a(6) = x(3) + x(6) + x(9)
a(7) = x(1) + x(5) + x(9)
a(8) = x(3) + x(5) + x(7)
For i = 1 To 7
'判断各横向、纵向及斜向的数值
For j = i + 1 To 8
If a(i) <> a(j) Then
'遇到不等就跳出
TongGuo = False
Exit For
Else
TongGuo = True
End If
Next
Next
End If
Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)
'临时显示结果
End Sub
程序运行时没有结果,最后需要单击关闭按扭强制退出程序,真认人扫兴
,请高手们解答一下吧!
后来我更改了程序,但结果依然是令人感到遗憾……
为何会这样的,请高手指点指点吧!
Dim x(1 To 9) As Integer
Dim a(1 To 8) As Integer
Dim TongGuo As Boolean
Private Sub Command1_Click()
TongGuo = False
For s1 = 1 To 9
x(1) = s1
For s2 = 1 To 9
x(2) = s2
If Bs(2) = False Then GoTo bs2
'如果比较出相同的值时,就跳出不必要的下层比较,而继续上一层的循环!(有不同值时才断续
For s3 = 1 To 9
'往下层进行比较)
x(3) = s3
If Bs(3) = False Then GoTo bs3
For s4 = 1 To 9
x(4) = s4
If Bs(4) = False Then GoTo bs4
For s5 = 1 To 9
x(5) = s5
If Bs(5) = False Then GoTo bs5
For s6 = 1 To 9
x(6) = s6
If Bs(6) = False Then GoTo bs6
For s7 = 1 To 9
x(7) = s7
If Bs(7) = False Then GoTo bs7
For s8 = 1 To 9
x(8) = s8
If Bs(8) = False Then GoTo bs8
For s9 = 1 To 9
x(9) = s9
If Bs(9) = False Then GoTo bs9
bbs
If TongGuo = True Then
For i = 0 To 8
Text1(i).Text = x(i + 1)
Next
Exit Sub
End If
bs9:
Next
bs8:
Next
bs7:
Next
bs6:
Next
bs5:
Next
bs4:
Next
bs3:
Next
bs2:
Next
Next
End Sub
Function Bs(pd As Integer) As Boolean
'每一层循环都与之前所有层的数字进行比较
For i = 1 To pd - 1
For j = i + 1 To pd
If x(i) <> x(j) Then
TongGuo = True
Else
TongGuo = False
Exit For
End If
Next
Next
Bs = TongGuo
Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)
End Function
Private Sub bbs()
If TongGuo = True Then
For i = 0 To 8
Text2(i).Text = x(i + 1)
Next
a(1) = x(1) + x(2) + x(3)
a(2) = x(4) + x(5) + x(6)
a(3) = x(7) + x(8) + x(9)
a(4) = x(1) + x(4) + x(7)
a(5) = x(2) + x(5) + x(8)
a(6) = x(3) + x(6) + x(9)
a(7) = x(1) + x(5) + x(9)
a(8) = x(3) + x(5) + x(7)
For i = 1 To 7
For j = i + 1 To 8
If a(i) <> a(j) Then
TongGuo = False
Exit For
Else
TongGuo = True
End If
Next
Next
End If
Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)
End Sub
[
本帖最后由 shi781203 于 2011-9-6 10:32 编辑 ]