数组乘以3的幂

时间:2014-02-03 11:23:44

标签: arrays vb.net

以下代码加载数组并乘以2的幂

Sub Main()

    Console.WriteLine("-EXAMPLE-" & vbNewLine & vbNewLine)

    Dim array(24) As Double, i As Long

    array(0) = 1
    For i = 1 To 24
        array(i) = 2 * array(i - 1)
    Next i
    Call DisplayArray(array)
End Sub
Sub DisplayArray(ByRef array() As Double)
    Dim i As Long, n As Long

    n = array.GetLength(0)
    For i = 0 To n - 1
        Console.WriteLine(array(i) & vbNewLine & vbNewLine)
    Next i
End Sub

如何调整代码使其达到3的幂?

我已将值'2'更改为'3'但仍然没有运气

2 个答案:

答案 0 :(得分:2)

我得到一个印象,你想要的数字是3的幂,如果是这样的话,你的数组中的元素将是1,3,9,27,81等,(3的幂)如果是逻辑下面的情况应该有效。

    array(0) = 1
    For i = 1 To 24
        array(i) = 3 * array(i - 1)
    Next i

或者你也可以这样做......

    For i = 0 To 24
        array(i) = Math.Pow(3, i)
    Next i

但是如果你期望你的数组被填充1,3,6,9等(3的倍数),那么下面的代码应该可以工作。

    array(0) = 1
    For i = 1 To 24
        array(i) = 3 * i
    Next i

答案 1 :(得分:0)

像这样 导入system.math

Sub Main()

    Console.WriteLine("-EXAMPLE-" & vbNewLine & vbNewLine)

    Dim array(24) As Double, i As Long

    array(0) = 1
    For i = 1 To 24
        array(i) = Math.Pow(3, array(i-1))
    Next i
    Call DisplayArray(array)
End Sub
Sub DisplayArray(ByRef array() As Double)
    Dim i As Long, n As Long


    For i = 0 To array.count-1
        Console.WriteLine(array(i) & vbNewLine & vbNewLine)
    Next i
End Sub