我怎么能输出数组中的六个数字,因为它只给我零

时间:2014-05-18 09:04:02

标签: arrays vb.net

Sub Main()
    Dim Num1 As Byte
    Dim Num2 As Byte
    Dim Num3 As Byte
    Dim Num4 As Byte
    Dim Num5 As Byte
    Dim Num6 As Byte
    Dim arrValue(5) As Integer
    Dim result As String = String.Join("", arrValue)

    Randomize()
    Num1 = CInt(Math.Ceiling(Rnd() * 40))
    Console.WriteLine("Num1:" & Num1)
    arrValue(0) = Num1

    Randomize()
    Num2 = CInt(Math.Ceiling(Rnd() * 40))
    arrValue(1) = Num2
    Console.WriteLine("Num2:" & Num2)

    Randomize()
    Num3 = CInt(Math.Ceiling(Rnd() * 40))
    Console.WriteLine("Num3:" & Num3)
    arrValue(2) = Num3


    Randomize()
    Num4 = CInt(Math.Ceiling(Rnd() * 40))
    Console.WriteLine("Num4:" & Num4)
    arrValue(3) = Num4

    Randomize(Num5)
    Num5 = CInt(Math.Ceiling(Rnd() * 40))
    Console.WriteLine("Num5:" & Num5)
    arrValue(4) = Num5

    Randomize()
    Num6 = CInt(Math.Ceiling(Rnd() * 40))
    Console.WriteLine("Num6:" & Num6)
    arrValue(5) = Num6

    Console.WriteLine("" & result)
    Console.ReadLine()
End Sub

2 个答案:

答案 0 :(得分:1)

仔细检查代码中的时间轴:

  • 何时将内容分配给result
  • 何时将值分配给数组?

提示:此代码从上到下执行。

答案 1 :(得分:1)

Dim result As String = String.Join("", arrValue)

应该移到

之前
Console.WriteLine("" & result)

成为

Dim result As String
...
result = String.Join("", arrValue)
Console.WriteLine("" & result)

因为您在为它们创建值之前从值中创建了字符串。