vb.net多维数组

时间:2017-01-19 00:26:28

标签: vb.net multidimensional-array

net现在关于多维数组我的问题是我想得到最接近的数字,例如我的输入是13000,最接近的数字是11667然后它将它减去13000,然后它将乘以0.25并添加我的1875.00。这是关于多维数组的代码,请你帮忙解决一下这个问题?我在项目中需要这个,提前谢谢你

    If ComboBox5.Text = "Z" Then
        Dim zero(3, 8) As Double
        Dim row As Double

        Dim col As Double

        'col 1
        zero(0, 0) = 0.0
        zero(1, 0) = 0
        zero(2, 0) = 1

        'col 2
        zero(0, 1) = 0.0
        zero(1, 1) = 0.05
        zero(2, 1) = 0

        'col 3
        zero(0, 2) = 41.67
        zero(1, 2) = 0.1
        zero(2, 2) = 833

        'col 4
        zero(0, 3) = 208.33
        zero(1, 3) = 0.15
        zero(2, 3) = 2500

        'col 5
        zero(0, 4) = 708.33
        zero(1, 4) = 0.2
        zero(2, 4) = 5833

        'col 6
        zero(0, 5) = 1875.0
        zero(1, 5) = 0.25
        zero(2, 5) = 11667

        'col 7
        zero(0, 6) = 4166.67
        zero(1, 6) = 0.3
        zero(2, 6) = 20833

        'col 8
        zero(0, 7) = 10416.67
        zero(1, 7) = 0.32
        zero(2, 7) = 41667


        For row = 0 To 3
            For col = 0 To 8

                If Val(TextBox18.Text) > Val(zero(row, col)) And Val(TextBox18.Text) < Val(zero(row - 1, col + 1)) Then
                    ListView1.Items.Add(zero(row, col))
                End If
            Next
        Next



    End If

1 个答案:

答案 0 :(得分:0)

2D数组不太适合存储此类数据,但这是一个让您入门的示例:

    Dim data(,) = {
        {0.0, 0, 1},
        {0.0, 0.05, 0},
        {41.67, 0.1, 833},
        {208.33, 0.15, 2500},
        {708.33, 0.2, 5833},
        {1875.0, 0.25, 11667},
        {4166.67, 0.3, 20833},
        {10416.67, 0.32, 41667}}

    Dim value = 13000 ' Dim value = Val(TextBox18.Text)

    ' data.GetLength(0) is 8, so Enumerable.Range generates the numbers from 0 to 7
    Dim range = Enumerable.Range(0, data.GetLength(0))

    ' .OrderBy orders them by the difference between value and the last column in data
    Dim ordered = range.OrderBy(Function(i) Math.Abs(data(i, 2) - value)) ' 5,4,6,3,2,0,1,7 

    Dim nearestIndex = ordered.First ' 5

    Dim value1 = data(nearestIndex, 0) ' 1875.0
    Dim value2 = data(nearestIndex, 1) ' 0.25
    Dim value3 = data(nearestIndex, 2) ' 11667

    Dim result = (value3 - value) * value2 + value1 ' result = 1541.75