模式、平均值、平均值等计算器,误差超出范围



我得到错误:

  • 线路:23
  • 字符:4
  • 下标超出范围:"numbers"
  • 即:如果数字(i)=a,则

我用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代码。

如果要声明变量(对于生产代码来说是个好主意):使用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是默认值,BTW,因此可以省略。

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

dim numbers2(99999)

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

numbers2 = c

那条线可能是

numbers2(i) = c

相关内容

  • 没有找到相关文章

最新更新