2015-10-26 40 views

回答

0

您可以通過隨機化數組示例中的項目總數(如果項目爲10)來完成此操作,然後先將其隨機化並將其傳遞給變量。稍後使用該變量來調用數組值。複製下面的代碼並粘貼在活動創建Sub和運行

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)) 
+0

謝謝!這對我幫助很大! :) –

+0

很高興我能夠幫助 – boluvisako

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)