回 帖 发 新 帖 刷新版面

主题:急切求助:此函数为何不对?

Public Function Max([color=FF0000]ByVal a() As Single[/color]) As Integer
        Dim d As Single
        Dim Len As Integer
        Dim i As Integer
        d = a(1)
        Len = a().length
        d = a(1)
        For i = 1 To Len
            If a(i) > d Then
                d = a(i)
            End If
        Next
        Return d
    End Function
    

我欲求一任意长度数组的最大值,函数程序如上,但提示a()存在问题?
请问:为何?
谢谢!

回复列表 (共5个回复)

沙发

Public Function Max(ByVal a() As integer) As Integer
        Dim d As integer
        Dim Len As Integer
        Dim i As Integer
        d = a(1)
        Len = a().length
        d = a(1)
        For i = 1 To Len
            If a(i) > d Then
                d = a(i)
            End If
        Next
        Return d
    End Function

板凳

1楼的程序我看也有问题

下面的是我该的,不报错了

  Public Function Max(ByVal a() As Single) As Integer
        Dim d As Single
        Dim Len As Integer
        Dim i As Integer
        d = a(1)
        Len = a.Length
        d = a(1)
        For i = 1 To Len
            If a(i) > d Then
                d = a(i)
            End If
        Next
        Return d
    End Function

主要问题是求数组的长度,不用带上括号
a()改成a 就ok 了。
答完,收工。。。。。。

3 楼

在调用时,是写
Max(b)
还是写
Max(b())
谢谢!b()为一数组。

4 楼

Max(b)
因为你调用的是数组,如果写成Max(b()),
编译器会认为你要传的是数组中的一个数据
但还是没有指定是第几个,所以就会报错了。

5 楼

你可以这样设定
Dim b() As Single
Max(b)
这样就可以了

我来回复

您尚未登录,请登录后再回复。点此登录或注册