回覆列表
  • 1 # a不會愛的小笨蛋

    我是用Excel中的宏做測試的,測試透過。你只需要將debug.print修改成print,然後執行test這個過程就可以了。 可能不是非常簡便,我很久沒有接觸vb了,下面是所有程式 Option Explicit Sub test() Dim i As Integer Dim Arr(1 To 10) As Single "用陣列存放隨機取到的數 Const MaxBound As Integer = 101 "因為隨機數Rnd取不到上界,因此要包含100的話需要把最大值設為101 Const MinBound As Integer = 30 "隨機數Rnd取得到下界 Dim SumNumber As Integer "存放陣列所有值的和 Dim AvgNumber As Single "存放資料的平均值 Dim MaxNumber As Integer "存放陣列中最大的值 Dim MinNumber As Integer "存放陣列中最小的值 Randomize "使每次取的隨機數都不相同 SumNumber = 0 "用迴圈隨機取10個數 For i = 1 To 10 Arr(i) = 30 + Int(Rnd * (MaxBound - MinBound)) "上界減去下界乘以一個隨機數並取整,這個就是你要的結果 Debug.Print "第" & i & "個數" & " " & Arr(i) "打印出隨機數 SumNumber = SumNumber + Arr(i) "累加,陣列的和 Next AvgNumber = SumNumber / 10 "求陣列平均值 Debug.Print "平均值=" & AvgNumber MaxNumber = Arr(1) For i = 2 To 10 If Arr(i) > MaxNumber Then MaxNumber = Arr(i) "判斷最大值 Next Debug.Print "最大值=" & MaxNumber MinNumber = Arr(1) For i = 2 To 10 If Arr(i) < MinNumber Then MinNumber = Arr(i) "判斷最小值 Next Debug.Print "最小值=" & MinNumber Debug.Print "-------------------------" End Sub

  • 中秋節和大豐收的關聯?
  • 脊髓炎後遺症會反反覆覆不斷復發嗎?