VB:定义并调用函数,求任意三个整数中的最大值

2026年09月23日 21:23
有2个网友回答
网友(1):

很基础的问题嘛,在窗体上放一个命令按钮command1,然后在按钮的单击事件里添加以下代码
Private Sub Command1_Click()
Dim a(1 To 3) As Double, i As Integer
For i = 1 To 3
a(i) = InputBox("请输入一个数字!", "提示")
Print a(i)
Next i
Print "最大数是" & largenum(a)
End Sub

Function largenum(a() As Double) As Double
Dim i As Integer, max As Double
max = a(1)
For i = 1 To 3
If max < a(i) Then max = a(i)
Next i
largenum = max
End Function

网友(2):

你是想自己写函数还是只是单纯的调用max()函数
如果是自己写的话就
function xx()
dim a,b,c,d as variant '根据你的需要设置数值类型吧
if a>b then
d=a
else:d=b
end if
if c>d then
d=c
end if
msgbox("3个数中最大的是:" & d)