永发信息网

vb编写一个具有可变参数的函数过程,可以求任意多个数的最大值,并用print方法调用该函数过程进行测试。

答案:3  悬赏:20  手机版
解决时间 2021-03-09 07:40
  • 提问者网友:遮云壑
  • 2021-03-08 08:03
vb编写一个具有可变参数的函数过程,可以求任意多个数的最大值,并用print方法调用该函数过程进行测试。
最佳答案
  • 五星知识达人网友:一把行者刀
  • 2021-03-08 08:56
Private Sub Command1_Click()
  Dim n As Integer
  n = CInt(InputBox("要计算几个数的平均值:"))
  Print "求 " & n & " 个数的平均值:";
  Print "平均值="; pj(n)
End Sub


Function pj(n As Integer) As Double
  For i = 1 To n
    x = Val(InputBox("第 " & i & " 个数是:"))
    Print x;
    s = s + x
  Next i
  pj = s / n
  Print
End Function

来自:求助得到的回答
全部回答
  • 1楼网友:神的生死簿
  • 2021-03-08 10:35
您好,您这样:
Dim Max As Integer, Min As Integer, MaxABC As String, MinABC As String
Private Sub Command1_Click()
Call MaxAndMin '调用过程计算一次
Picture1.Cls '清除PictureBox中的内容
Picture1.Print "最大值是" & MaxABC & "," & MaxABC & "=" & Max
End Sub
Private Sub Command2_Click()
Call MaxAndMin
Picture1.Cls
Picture1.Print "最小值是" & MinABC & "," & MinABC & "=" & Min
End Sub
Private Sub MaxAndMin()
'定义3个变量存放3个text的值,并计算最大和最小值
Dim Num1 As Integer, Num2 As Integer, Num3 As Integer
Picture1.AutoRedraw = True
Num1 = Val(Text1.Text): Num2 = Val(Text2.Text): Num3 = Val(Text3.Text)
Max = Num1: MaxABC = "A"
Min = Num1: MinABC = "A"
If Max < Num2 Then Max = Num2: MaxABC = "B"
If Max < Num3 Then Max = Num3: MaxABC = "C"
If Min > Num2 Then Min = Num2: MinABC = "B"
If Min > Num3 Then Min = Num3: MinABC = "C"
End Sub
  • 2楼网友:舍身薄凉客
  • 2021-03-08 09:09
我想你指参数可多可少那种吧。
Private Sub Command1_Click()
MsgBox test(4, 3, 8, 7)
MsgBox test(4, 3, 45, 5, 8, 10, 8, 7)
End Sub
Function test(ParamArray items()) As Double
If UBound(items) >= 0 Then
Dim maxIndex As Integer
maxIndex = 0
Dim i As Integer
For i = 1 To UBound(items)
If Val(items(i)) > Val(items(maxIndex)) Then maxIndex = i
Next
test = items(maxIndex)
End If
End Function
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯