这个练习是定义一个通用过程caljc,求数组的和、最大值、最小值
在form_load()中输入数组的值,然后调用,很简单的练习,但是出了一个错误不知道是怎么回事?
执行的时候显示错误:  编译错误:数据类型不匹配:缺少数组或用户定义类型
可是数组和类型都没问题呀?大家帮我找一下错,谢谢了!
______________________________________________________________
Sub caljc(k As Integer, darray() As Integer, s As Long, _
 m As Integer, n As Integer)
Dim i As Integer
s = darray(1): m = darray(1): n = darray(1)
If k = 1 Then Exit Sub
For i = 2 To k
    s = darray(i) + s
    If darray(i) > m Then m = darray(i)
    If darray(i) < n Then n = darray(i)
Next i
End Sub
________________________________________________________________
Private Sub Form_Load()
Show
Dim jc(), x, n As Integer, sum As Long, max As Integer, _
 min As Integer
n = 0
Do While True
    x = Val(InputBox("请输入第" & n + 1 & "个学生的成绩(-1结束)"))
    If x = -1 Then Exit Do
    n = n + 1
    jc(n) = x
LoopIf n > 0 Then
    Call caljc(n, jc(), sum, max, min)
Else
    End
End IfPrint "平均分:"; Format(sum / n, "###.0")
Print "最高分:"; max
Print "最低分:"; min
End Sub

解决方案 »

  1.   


    Sub caljc(k As Integer, darray() As Integer, s As Long, m As Integer, n As Integer)
        Dim i As Integer
        s = darray(1): m = darray(1): n = darray(1)
        If k = 1 Then Exit Sub
        For i = 2 To k
            s = darray(i) + s
            If darray(i) > m Then m = darray(i)
            If darray(i) < n Then n = darray(i)
        Next i
    End SubPrivate Sub Form_Load()
        Show
        Dim jc() As Integer, x As Integer, n As Integer, sum As Long, max As Integer, min As Integer
        ReDim jc(1)
        n = 0
        Do While True
            x = Val(InputBox("请输入第" & n + 1 & "个学生的成绩(-1结束)"))
            If x = -1 Then Exit Do
            n = n + 1
            ReDim Preserve jc(n + 1)
            jc(n) = x
        Loop    If n > 0 Then
            Call caljc(n, jc(), sum, max, min)
        Else
            End
        End If    Print "平均分:"; Format(sum / n, "###.0")
        Print "最高分:"; max
        Print "最低分:"; min
    End Sub
      

  2.   

    楼上的两位:添加redim jc(1)也不行呀!还是以前的错误!
    把jc()改成jc(100)也不行Dim jc(), x, n As Integer, sum As Long, max As Integer, _
     min As Integer
      

  3.   

    Dim jc(1000) As Integer
    要定义成Integer类型
      

  4.   

    你要定义成动态类型的数组.在附值的时候用ReDim Preserve jc(UBound(gTraps) + 1)来附值吧
      

  5.   

    问:tztz520(午夜逛街)
    dim jc(1000),n,x as integer
    不是也把jc()定义成 integer 了吗?
    看来是我把这里弄错了!
    谢谢各位!
      

  6.   

    1、你没有分配数组大小,而且不知道数组类型适合类型
    2、Call caljc(n, jc(), sum, max, min)去掉数组括号
    3、Sub caljc(k As Integer, darray() As Integer, s As Long, _
     m As Integer, n As Integer)突然数组变成了integer类型
    4、即便以上三点改过来了也不行,错的地方太多了!
    给你一个例子参考:窗体上放个按钮,然后粘贴代码:
    Private Sub Command1_Click()
    '定义数组
     Dim a(4) As Integer
     Dim s As Single, m As Integer, n As Integer
     a(0) = 99
     a(1) = 89
     a(2) = 100
     a(3) = 96
     a(4) = 91
     Caljc a, s, m, n
     MsgBox "合计值:" & s
     MsgBox "最大值:" & m
     MsgBox "最小值:" & n
    End Sub
    Sub Caljc(a As Integer, s As Single, m As Integer, n As Integer)
     '排列顺序
     Dim i, j, iTmp As Integer
     Dim TmpVal As Integer
     For i = 0 To UBound(a)
         iTmp = i
         TmpVal = a(i)
         For j = i + 1 To UBound(a)
            If a(j) > a(i) Then
               TmpVal = a(j)
               Exit For
            End If
            DoEvents
         Next j
         If j < 5 Then
           a(j) = a(i)
           a(i) = TmpVal
         End If
         DoEvents
     Next i
     '最大值
     m = a(0)
     '最小值
     n = a(UBound(a))
     '合计值
     For i = 0 To UBound(a)
       s = s + a(i)
       DoEvents
     Next i
    End Sub
      

  7.   

    应该是:
    Option Explicit
    Private Sub Command1_Click()
    '定义数组
     Dim a(4) As Integer
     Dim s As Single, m As Integer, n As Integer
     a(0) = 99
     a(1) = 89
     a(2) = 100
     a(3) = 96
     a(4) = 91
     Caljc a, s, m, n
     MsgBox "合计值:" & s
     MsgBox "最大值:" & m
     MsgBox "最小值:" & n
    End Sub
    Sub Caljc(a() As Integer, s As Single, m As Integer, n As Integer)
     '排列顺序
     Dim i, j, iTmp As Integer
     Dim TmpVal As Integer
     For i = 0 To UBound(a)
         iTmp = i
         TmpVal = a(i)
         For j = i + 1 To UBound(a)
            If a(j) > a(i) Then
               TmpVal = a(j)
               Exit For
            End If
            DoEvents
         Next j
         If j < 5 Then
           a(j) = a(i)
           a(i) = TmpVal
         End If
         DoEvents
     Next i
     '最大值
     m = a(0)
     '最小值
     n = a(UBound(a))
     '合计值
     For i = 0 To UBound(a)
       s = s + a(i)
       DoEvents
     Next i
    End Sub