0

我得到错误:

  • 线路:23
  • 字符:4
  • 下标超出范围:“数字”
  • 位于:如果 numbers(i) = a then

我用 javascript 做了这个,它可以工作,但我需要转换它。

function info(numbers)
    dim numbers2(99999)
    numbers3 = ""
    dim low
    dim high
    dim mean
    dim halve
    total = 0
    dim spaces(99999)
    dim a
    dim b
    dim i
    dim c
    dim whole
    dim meadian
    dim mode1
    dim mode2
    dim intResult
    dim med
    x = UBound(numbers)+1
    For a=0 To 999999 Step 1
        For i=0 To x Step 1
            if numbers(i) = a then
                c = numbers(i) 
                numbers2 = c
                numbers3 = numbers3 + c + " "
                low = numbers2(0)
                high = a
                total = total + c
            end if
    Next
    Next
    halve = UBound(numbers2)/2
    whole =  UBound(numbers2)
    intResult = whole Mod 2
    If intResult = 0 Then
        halve = halve - 0.5
        median = numbers2(halve)
        med = true
    Else
        median = (numbers2(halve1)+numbers2(halve1-1))/2
        med = false
    End if
    mean = total / UBound(numbers)
    if med = true then
        msgbox(numbers3 & chr(13) & chr(13) & "Lowest:  " & low & chr(13) & "Highest: " & high & chr(13) & "Total:   " & total & chr(13) & "Median:  " & median & chr(13))
    else
        msgbox(numbers3 & chr(13) & chr(13) & "Lowest:  " & low & chr(13) & "Highest: " & high & chr(13) & "Total:   " & total & chr(13) & "Median:  " & median & "   -" & numbers2(halve1) & "x" & numbers2(halve1-1) & chr(13))
    end if
end function
dim q(19,291,29)
info(q)

而且,我怎样才能把 q 放在输入框中?只需询问您是否想要javascript代码。

4

1 回答 1

2

如果您要声明变量(生产代码的好主意):使用Option Explicit并声明所有变量。否则不要打扰。

如果将变量声明放在一行中(逗号分隔),它们会变得更易读:

Dim numbers2(99999), numbers3, low, high, mean, halve, total
Dim spaces(99999), ...
...

numbers3 = ""
total = 0

正如@RogerRowland 所提到的,该行将x = UBound(numbers)+1返回一个大于数组上边界的索引 1,因此循环

For i=0 To x Step 1
  if numbers(i) = a then
    ...
  end if
Next

将尝试在最后一个循环周期中访问数组外的元素。最好这样做:

For i=0 To UBound(numbers)
  If numbers(i) = a Then
    ...
  End If
Next

Step 1是默认值,顺便说一句,因此可以省略。

修复后出现的类型不匹配错误很可能是因为您将 numbers2 声明为静态数组:

dim numbers2(99999)

但然后在循环内为其分配一个标量:

numbers2 = c

那条线应该是

numbers2(i) = c
于 2013-11-22T09:42:02.623 回答