0
我對Basic4Android編程非常陌生。我只想知道如何從數組中繪製一個隨機數。如何從basic4android中的數組中繪製隨機數?
我對Basic4Android編程非常陌生。我只想知道如何從數組中繪製一個隨機數。如何從basic4android中的數組中繪製隨機數?
您可以通過隨機化數組示例中的項目總數(如果項目爲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))
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)
謝謝!這對我幫助很大! :) –
很高興我能夠幫助 – boluvisako