这么简单的vb过程怎么会错啊 不爽啊

来源:百度知道 编辑:UC知道 时间:2024/07/02 05:13:11
Function a() As Integer
Dim q As Integer, m As Integer, n As Integer
m = 0
n = 0
q = 0
Randomize
For i = 1 To 100
a(i) = Int(10 + 90 * Rnd)
If a(i) <= 40 Then
m = m + 1
If 40 < a(i) <= 70 Then
n = n + 1
If a(i) > 70 Then
q = q + 1
End If
End If
End If
Next
End Function

Private Sub Command1_Click()
MsgBox m
End Sub

msgbox 什么也不显示啊 为什么啊
能不能详细点啊 该怎么改啊

m是作用域限于函数a()的局部变量。

在过程Command1_Click()中m无效。相当于重新声明了一个变量。

解决方法1.把m定义成private,放在这两个过程之外
2.让函数返回m

1.function没返回值
2.m没定义

m不是没定义 只是没定义为全局变量而已~
function不必有返回值 因为它要的结果是m值

第一,m不是全局变量,第二函数返回的值只是同名变量的值。你的意思是传个什么值。参考下面的

=======================================

Dim q As Integer, m As Integer, n As Integer, a(100)
Function b() As Integer

m = 0
n = 0
q = 0
Randomize
For i = 1 To 100
a(i) = Int(10 + 90 * Rnd)
If a(i) <= 40 Then
m = m + 1
If 40 < a(i) <= 70 Then
n = n + 1
If a(i) > 70 Then
q = q + 1
End If
End If
End If
Next
b = m
End Function

Private Sub Command1_Click()
MsgBox b
End Sub

不用误导人嘛。。q,m,n,a() 都只是在Function内用到。放里面去啊。。

Function b() As Integer
Dim q As Integer, m As Integer, n As Integer, a(100)
m = 0
n = 0