我用vb function函数编写一个比较大小的代码如下

Public Function fmax(aAs Integer, b As Integer, c As Integer)
Dim t As Integer
If a > b Then
t = a
Else
t = b
End If
If t > c Then
fmax = c
End If

End Function

Private SubForm_click()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim max As Integer
a = 1
b = 2
c = 3
max = fmax(a, b, c)
Print max
End Sub
理想中的结果是单击后显示为3怎么结果是0

第1个回答  2013-08-18
If t > c Then
fmax = c
End If

这一段,比较条件有误,求最大值,应该是 if t<c then,才能找到较大的那个。
另外,fmax=c这句有问题,因为,如果t>c时,fmax应该是t,而你的程序段中,没有考虑到这种情况,就出现了当c是最大值的时候fmax未被赋值,输出为默认初始值0的情况。
破天的代码是正确的。
先用中间值t存储三个数中最大的一个,然后,将t赋值给fmax,这样避免出现未赋值的情况。
第2个回答  2013-08-18
现在a,b,c的值为1,2,3
执行函数体中的语句,判断if a>b then,a为1,b为2,a>b不成立。
执行else下面语句t=b,此时t的值为2
然后判断if t>c then。t的值为2,c的值为3。t>c不成立,if下面的语句fmax=c不被执行,所以fmax函数没有得到值,那么就返回0了

If t > c Then
fmax = c
End If
应改为:
If t < c Then
t = c
End If
fmax=t追问

怎么点击后出现fmax不可选

第3个回答  推荐于2016-09-11
正确的函数编写如下:
Public Function fmax(a As Integer, b As Integer, c As Integer)
if a > b Then
fmax = a
Else
fmax = b
End If
If c>fmax Then
fmax = c
End If本回答被提问者和网友采纳
第4个回答  2013-08-19
public function Max(a,b,c)
dim t

t =a

if b> t Then t = b

if c> t Then t =c

max =t

end function