| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 716 人关注过本帖
标题:菜鸟自己做的VB程序,不知道错在哪里了?
只看楼主 加入收藏
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
菜鸟自己做的VB程序,不知道错在哪里了?
Public a As Integer
Public b As Integer
Public c As Integer
Public d As Integer
Public e As Integer
Public f As Integer
Function GetLen(m As Variant, n As Variant) As Integer
If m = 0 And n = 1 Then
GetLen = a
ElseIf m = 0 And n = 2 Then
GetLen = b
ElseIf m = 0 And n = 3 Then
GetLen = c
ElseIf m = 1 And n = 0 Then
GetLen = a
ElseIf m = 2 And n = 0 Then
GetLen = b
ElseIf m = 3 And n = 0 Then
GetLen = c
ElseIf (m * 10 + n) = 12 Or (m * 10 + n) = 21 Then
GetLen = d
ElseIf (m * 10 + n) = 13 Or (m * 10 + n) = 31 Then
GetLen = e
ElseIf (m * 10 + n) = 23 Or (m * 10 + n) = 32 Then
GetLen = f
Else
GetLen = -1000
End If
End Function

Function LnLen(Arr(), i As Integer) As Integer
Dim j As Integer
LnLen = 0
For j = 0 To i - 2
LnLen = LnLen + GetLen(Arr(j), Arr(j + 1))
Next j
End Function


Private Sub Command1_Click()
a = Val(Text1.Text): b = Val(Text2.Text): c = Val(Text3.Text): d = Val(Text4.Text): e = Val(Text5.Text): f = Val(Text6.Text)
If a <= 0 Or b <= 0 Or c <= 0 Or d <= 0 Or e <= 0 Or f <= 0 Then MsgBox "距离输入有误!", vbOKOnly + vbCritical, "提示"
Dim M1()
Dim M2()
Dim M3()
Dim M4()
Dim M5()
Dim M6()
Dim N1()
Dim N11()
Dim N12()
Dim N2()
Dim N21()
Dim N22()
Dim N3()
Dim N31()
Dim N32()
Dim O1()
Dim O2()
Dim O3()
Dim cmp(1 To 13) As Integer, i As Integer, j As Integer, k As Integer
M1 = Array(0, 1, 2, 3, 0): M2 = Array(0, 1, 3, 2, 0): M3 = Array(0, 2, 1, 3, 0): M4 = Array(0, 2, 3, 1, 0): M5 = Array(0, 3, 1, 2, 0): M6 = Array(0, 3, 2, 1, 0)
N1 = Array(0, 1, 0): N11 = Array(0, 2, 3, 0): N12 = Array(0, 3, 2, 0)
N2 = Array(0, 2, 0): N21 = Array(0, 1, 3, 0): N22 = Array(0, 3, 1, 0)
N3 = Array(0, 3, 0): N21 = Array(0, 1, 2, 0): N22 = Array(0, 2, 1, 0)
O1 = Array(0, 1, 0): O2 = Array(0, 2, 0): O3 = Array(0, 3, 0)
Text7.Text = "0,1,2,3,0": Text8.Text = LnLen(M1, 5): cmp(1) = LnLen(M1, 5)
Text9.Text = "0,1,3,2,0": Text10.Text = LnLen(M2, 5): cmp(2) = LnLen(M2, 5)
Text11.Text = "0,2,1,3,0": Text12.Text = LnLen(M3, 5): cmp(3) = LnLen(M3, 5)
Text13.Text = "0,2,3,1,0": Text14.Text = LnLen(M4, 5): cmp(4) = LnLen(M4, 5)
Text15.Text = "0,3,1,2,0": Text16.Text = LnLen(M5, 5): cmp(5) = LnLen(M5, 5)
Text17.Text = "0,3,2,1,0": Text18.Text = LnLen(M6, 5): cmp(6) = LnLen(M6, 5)
Text19.Text = "0,1,0,0,2,3,0": Text20.Text = LnLen(N1, 3) + LnLen(N11, 4): cmp(7) = LnLen(N1, 3) + LnLen(N11, 4)
Text21.Text = "0,1,0,0,3,2,0": Text22.Text = LnLen(N1, 3) + LnLen(N12, 4): cmp(8) = LnLen(N1, 3) + LnLen(N12, 4)
Text23.Text = "0,2,0,0,1,3,0": Text24.Text = LnLen(N2, 3) + LnLen(N21, 4): cmp(9) = LnLen(N2, 3) + LnLen(N21, 4)
Text25.Text = "0,2,0,0,3,1,0": Text26.Text = LnLen(N2, 3) + LnLen(N22, 4): cmp(10) = LnLen(N2, 3) + LnLen(N22, 4)
Text27.Text = "0,3,0,0,1,2,0": Text28.Text = LnLen(N3, 3) + LnLen(N31, 4): cmp(11) = LnLen(N3, 3) + LnLen(N31, 4)
Text29.Text = "0,3,0,0,2,1,0": Text30.Text = LnLen(N3, 3) + LnLen(N32, 4): cmp(12) = LnLen(N3, 3) + LnLen(N32, 4)
Text31.Text = "0,1,0,0,2,0,0,3,0": Text32.Text = LnLen(O1, 3) + LnLen(O2, 3) + LnLen(O3, 3): cmp(13) = LnLen(O1, 3) + LnLen(O2, 3) + LnLen(O3, 3)
j = 13: k = cmp(13)
For i = 12 To 1 Step -1
If cmp(i) < k Then k = cmp(i): j = i
Next i
Text33.Text = j
End Sub

Private Sub Command2_Click()
Text1.Text = "1": Text2.Text = "1": Text3.Text = "1": Text4.Text = "1": Text5.Text = "1": Text6.Text = "1"
Text7.Text = "": Text8.Text = "": Text9.Text = "": Text10.Text = "": Text11.Text = "": Text12.Text = "": Text13.Text = ""
Text14.Text = "": Text15.Text = "": Text16.Text = "": Text17.Text = "": Text18.Text = "": Text19.Text = "": Text20.Text = ""
Text21.Text = "": Text22.Text = "": Text23.Text = "": Text24.Text = "": Text25.Text = "": Text26.Text = "": Text27.Text = ""
Text28.Text = "": Text29.Text = "": Text30.Text = "": Text31.Text = "": Text32.Text = "": Text33.Text = ""
End Sub

Private Sub Command3_Click()
End
End Sub

Private Sub Timer1_Timer()
Text34.Text = Now
End Sub
小弟的问题是红色部分编译出错说,数组下标越界(LnLen = LnLen + GetLen(Arr(j), Arr(j + 1))),下面这句Function LnLen(Arr(), i As Integer) As Integer,i是Arr数组元素的个数,怎么会错,我不知道是什么意思。另外不知道数组怎么做一个function的参数,有什么要注意的,我用的是VB6.0。请高手指教,谢谢。

[ 本帖最后由 qq1023569223 于 2010-12-22 15:06 编辑 ]
2010-12-22 14:40
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:10 
LnLen = LnLen + GetLen(Arr(j), Arr(j + 1))
       
给值    引用,参与计算


在VB里,函数里,函数名的值是用来返回的。
那么当你在函数里,仍然使用  引用函数名 时,那么系统是使用你当前值,还是认为你需要重新调用一个程序呢?
VB 里,是认为你重新调用一个程序。
所以这里,在 引用并参与计算时,就会出错。

这里修改意见:
函数里 值使用 一个变量保存,直到最后返回时,再把 变量值给 函数名。

授人于鱼,不如授人于渔
早已停用QQ了
2010-12-22 15:34
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
Function LnLen(Arr(), i As Integer) As Integer
Dim j As Integer, k As Integer
For j = 0 To i - 2
k = k + GetLen(Arr(j), Arr(j + 1))
Next j
LnLen=k
VB_GIS.rar (3.89 KB)

End Function
我把程序改成这样,可是还是出错啊,老是说数组的下标越界了。我把工程都上传过来了,能麻烦再帮我看一下吗?谢谢。

   唯实惟新 至诚致志
2010-12-22 18:13
Artless
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:4211
专家分:28888
注 册:2009-4-8
收藏
得分:10 
N3 = Array(0, 3, 0): N31 = Array(0, 1, 2, 0): N32 = Array(0, 2, 1, 0)

无知
2010-12-22 19:41
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
回复 4楼 Artless
多谢各位了。终于知道错在哪里了.
Artless的提示让我知道粗心真是好可怕的,程序没有错,可是自己写错了,真是不应该。
风吹过b让我知道了一个书上没有提到的问题,非常感谢!其实我的VB是自己学的,很多地方没有注意哦。

   唯实惟新 至诚致志
2010-12-22 20:28
快速回复:菜鸟自己做的VB程序,不知道错在哪里了?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031113 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved