vb中显示“下标越界”是什么意思?为什么我这个程序总会显示下标越界?谢谢
Dima(1To10)PrivateSubCommand1_Click()Print"排序前:";Fori=1To10a(i)=Int(Rnd*100+1)NextiFo...
Dim a(1 To 10)
Private Sub Command1_Click()
Print "排序前:";
For i = 1 To 10
a(i) = Int(Rnd * 100 + 1)
Next i
For i = 1 To 10
Print a(i);
Next i
End Sub
Private Sub Command2_Click()
Print "排序后:";
For i = 1 To 10
For j = i To 1 Step -1
If a(j) < a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next j
Print
Next i
For i = 1 To 10
Print a(i);
Next i
End Sub 展开
Private Sub Command1_Click()
Print "排序前:";
For i = 1 To 10
a(i) = Int(Rnd * 100 + 1)
Next i
For i = 1 To 10
Print a(i);
Next i
End Sub
Private Sub Command2_Click()
Print "排序后:";
For i = 1 To 10
For j = i To 1 Step -1
If a(j) < a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next j
Next i
For i = 1 To 10
Print a(i);
Next i
End Sub 展开
1个回答
展开全部
当j=10时,a(j + 1)=a(11),这就超出数组a的下标范围了,所以提示你“下标越界”。应该把排序的代码改为:
Private Sub Command2_Click()
Print "排序后:"
For i = 1 To 9
For j = i + 1 To 10
If a(i) < a(j) Then
t = a(i)
a(i) = a(j)
a(j) = t
End If
Next j
Next i
For i = 1 To 10
Print a(i);
Next i
End Sub
Private Sub Command2_Click()
Print "排序后:"
For i = 1 To 9
For j = i + 1 To 10
If a(i) < a(j) Then
t = a(i)
a(i) = a(j)
a(j) = t
End If
Next j
Next i
For i = 1 To 10
Print a(i);
Next i
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询