【问题标题】:How to draw random number from array in basic4android?如何从basic4android中的数组中提取随机数?
【发布时间】:2015-10-26 05:02:47
【问题描述】:

我对 Basic4Android 编程非常陌生。我只想知道如何从数组中提取随机数。

【问题讨论】:

    标签: arrays basic4android


    【解决方案1】:

    如果项目为 10,您可以通过随机化数组示例中的项目总数来做到这一点,然后先随机化并将其传递给变量。稍后使用该变量调用数组值。复制下面的代码并粘贴到Activity Create Sub and 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)) 
    

    【讨论】:

    • 谢谢!这对我帮助很大! :)
    • 很高兴我能提供帮助
    【解决方案2】:
    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)
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 2010-12-11
      • 2021-04-16
      • 2018-08-01
      相关资源
      最近更新 更多