标题:VB程序总是下标越界.看看为什么!
只看楼主
solitude1984
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-4-23
 问题点数:0 回复次数:8 
VB程序总是下标越界.看看为什么!

Private Sub Command1_Click()
Dim a, b, ni, c, ok, nmax, test As Integer
Dim cc, result, sum, suml, bound, G As Single
Dim p(2, 2) As Single
Dim q(2) As Single
Dim s(2) As Single
Dim s1(2) As Single
Dim N, M, i, j As Single
N = 2
M = 2
c = 1
result = 0
ni = 0
For i = 1 To N
For j = 1 To M
p(i, j) = InputBox("请输入数据")
Print p(i, j)
Next j
Next i
sum = 0
For j = 1 To M
For i = 1 To N
sum = sum + p(i, j)
Next i
Next j
If sum > 1 Then
Print "输入信道矩阵不正确,请重新输入信道矩阵!"
c = 1
i = N
End If
c = 0
For i = 1 To N 'a请输入迭代计算精度:'
q(i) = 1# / N
Next i
For i = 1 To N
Next i
s(i) = q(i)
For j = 1 To M
s(j) = 0
For i = 1 To N
s(j) = s(j) + s(i) * p(i, j)
Next i
Next j
For j = 1 To M
For i = 1 To N
p(i, j) = s(i) * p(i, j) / s(j)
Next i
Next j
End Sub

搜索更多相关主题的帖子: Dim VB Single sum Next 
2007-04-23 20:27
b13690976754
Rank: 1
等 级:新手上路
威 望:2
帖 子:835
专家分:7
注 册:2006-11-9
得分:0 
Private Sub Command1_Click()
Dim a, b, ni, c, ok, nmax, test As Integer
Dim cc, result, sum, suml, bound, G As Single
Dim p(2, 2) As Single
Dim q(2) As Single
Dim s(2) As Single
Dim s1(2) As Single
Dim N, M, i, j As Single
N = 2
M = 2
c = 1
result = 0
ni = 0
For i = 1 To N ‘ 你的For循环已经超过了你你定义的是(2, 2)
For j = 1 To M

p(i, j) = InputBox("请输入数据")
Print p(i, j)
Next j
Next i
sum = 0
For j = 1 To M
For i = 1 To N
sum = sum + p(i, j)
Next i
Next j
If sum > 1 Then
Print "输入信道矩阵不正确,请重新输入信道矩阵!"
c = 1
i = N
End If
c = 0
For i = 1 To N 'a请输入迭代计算精度:'
q(i) = 1# / N
Next i
For i = 1 To N
Next i
s(i) = q(i)
For j = 1 To M
s(j) = 0
For i = 1 To N
s(j) = s(j) + s(i) * p(i, j)
Next i
Next j
For j = 1 To M
For i = 1 To N
p(i, j) = s(i) * p(i, j) / s(j)
Next i
Next j
End Sub

If Dir(\"alive\") <> \"\" And Dir(\"ideal\") <> \" Then Print \"strive\" End If
2007-04-23 20:43
Joforn
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1242
专家分:122
注 册:2007-1-2
得分:0 
怎么你的代码是一路并排写下来的?看着好累啊…………

VB QQ群:47715789
2007-04-23 20:43
b13690976754
Rank: 1
等 级:新手上路
威 望:2
帖 子:835
专家分:7
注 册:2006-11-9
得分:0 
我随便看了一下 发现他For循环嵌套用错了 其他我也没看

If Dir(\"alive\") <> \"\" And Dir(\"ideal\") <> \" Then Print \"strive\" End If
2007-04-23 20:51
Joforn
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1242
专家分:122
注 册:2007-1-2
得分:0 

Private Sub Command1_Click()
Dim a, b, ni, c, ok, nmax, test As Integer
Dim cc, result, sum, suml, bound, G As Single
Dim p(2, 2) As Single
Dim q(2) As Single
Dim s(2) As Single
Dim s1(2) As Single
Dim N, M, i, j As Single

N = 2
M = 2
c = 1
result = 0
ni = 0
For i = 1 To N
For j = 1 To M
p(i, j) = InputBox("请输入数据")
Print "P(" & i & "," & j & ")=" & p(i, j)
Next j
Next i
sum = 0
For j = 1 To M
For i = 1 To N
sum = sum + p(i, j)
Next i
Next j
If sum > 1 Then
Print "输入信道矩阵不正确,请重新输入信道矩阵!"
c = 1
i = N
End If
c = 0
For i = 1 To N 'a请输入迭代计算精度:'
q(i) = 1# / N
Next i
For i = 1 To N
Next i
s(i) = q(i) '程序运行到此处i的值一定是等于N+1,也就是3,而你只定义了S(2)q(2),所以程序运行到此处时,数组一定会越界
For j = 1 To M
s(j) = 0
For i = 1 To N
s(j) = s(j) + s(i) * p(i, j)
Next i
Next j
For j = 1 To M
For i = 1 To N
p(i, j) = s(i) * p(i, j) / s(j)
Next i
Next j
End Sub


VB QQ群:47715789
2007-04-23 20:53
Joforn
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1242
专家分:122
注 册:2007-1-2
得分:0 
以下是引用b13690976754在2007-4-23 20:51:32的发言:
我随便看了一下 发现他For循环嵌套用错了 其他我也没看

你标记的两处楼主是正确的,这样用是不会越界的。

楼主你如果想把数组q里数据全部放入数组s里应该这样写
For i = 1 To N
s(i) = q(i)
Next i

[此贴子已经被作者于2007-4-23 21:04:11编辑过]


VB QQ群:47715789
2007-04-23 20:58
b13690976754
Rank: 1
等 级:新手上路
威 望:2
帖 子:835
专家分:7
注 册:2006-11-9
得分:0 
哈哈 没认真看

If Dir(\"alive\") <> \"\" And Dir(\"ideal\") <> \" Then Print \"strive\" End If
2007-04-23 22:26
solitude1984
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-4-23
得分:0 

呵呵!谢谢各位,我试了一下通过了!

2007-04-24 08:41
solitude1984
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-4-23
得分:0 

谢谢了!我是新手,刚学着自己写,准备作毕设的!在百度花钱都没人甩!大家辛苦了!就是还有一个问题,改正后次程序还会溢出提示为这里:s(j) = s(j) + s(i) * p(i, j)

2007-04-24 08:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-134341-1-1.html




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

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