帮忙写一个简单的vb程序1.2

2024-12-28 22:05:14
推荐回答(1个)
回答1:

程序中有几处错误:
1.你的本意是将a(1 To 10)作为全局变量的,而不是过程变量,因此你在Command1_Click中定义a(1 To 10)在其他过程中引用不了。
2.Command2_Click中你又定义了一遍a(1 To 10),也就是说不管前面a(1 To 10)的值如何,在这一过程中都被你重置为0了。

简单修改下:
1.调整下Dim a(1 To 10) As Integer的位置;
2.删除Command2_Click中的Dim a(1 To 10) As Integer;为便于发现错误,建议在程序前增加option explicit。
更改后的完成代码如下:

Option Explicit

Private a(1 To 10) As Integer, c, b '调整后的

Private Sub Command1_Click()
'Dim a(1 To 10) As Integer, c, b '原程序
Dim c, b '更改后的
Dim m As Integer
m = 100
c = GetArray(a(), m)
b = PntArray(a())
End Sub

Function GetArray(a() As Integer, ByVal m As Integer)
Dim i As Integer
For i = LBound(a) To UBound(a)
a(i) = Int(Rnd * m) '产生0~range范围的随机整数
Next i
End Function

Function PntArray(a() As Integer)
Dim i As Integer
For i = LBound(a) To UBound(a)
Print a(i);
Next i
Print
End Function

Private Sub Command2_Click()
Dim max As Integer
Dim min As Integer
Dim i As Integer
'Dim a(1 To 10) As Integer '被删除的
max = 0
min = 100
For i = LBound(a) To UBound(a)
If a(i) > max Then max = a(i)
If a(i) < min Then min = a(i)
Next i
Print "最大值:" & max
Print "最小值:" & min
End Sub

给分吧。