我对Basic4Android编程非常陌生。我只想知道如何从数组中画一个随机数。
发布于 2016-01-20 15:45:06
如果条目为10,则可以通过随机化数组示例中的项目总数来做到这一点,然后先将其随机化并传递给变量。稍后使用该变量调用数组值。复制下面的代码并粘贴到并运行
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)) 发布于 2017-04-21 21:43:58
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)https://stackoverflow.com/questions/33338775
复制相似问题