我是Basic4Android编程的新手。我只是想知道如何从数组中绘制一个随机数。
答案 0 :(得分:0)
如果项目为10,则可以通过随机化数组示例中的项目总数来实现,然后先随机化并将其传递给变量。稍后使用该变量来调用数组值。复制下面的代码并粘贴在Activity Create Sub和Run
中Dim myArray As List ' Declare your Array
myArray.Initialize() ' Initialize array
myArray.AddAll(Array As String("January","February","March","April"))
'Since array values index starts from zero, then four items in a list will be from 0 to 3.
'So randomize 0 to 4
Dim randNum As Int
randNum = Rnd(1,4) 'Generating random number
Log ("Current RAndom Number is " & randNum) 'This will print the random number
'=========PRINT RESULT TO LOGCAT ======
'Since we are generating from 1 to 4, we use -1 (4-1=3 ie April ==Array index starts from 0 to 3)
Log(myArray.Get(randNum-1))
答案 1 :(得分:0)
Dim arrayLength as int = 100 ' an arbitrary integer >0
Dim myArray(arrayLength) as int ' or double, float, long, byte...
' ... fill the array ...
Log(myArray(Rnd(0,arrayLength))) ' "Rnd" goes from 0 (incl) to arrayLength (excl)