✔ 最佳答案
用 For Loop 比較好,計算平方根可以用 VB 的 Sqr() Function.
Function summarion(ByVal count As Integer) As Double
Dim i As Integer
Dim loopCount As Integer
Dim total As Double
loopCount = count * 2
For i = 2 To loopCount Step 2
total = total + Sqr(-1 + i)
' 不用 Sqr() 的寫法
' total = total + (-1 + i) ^ 0.5
Next
summarion = total
End Function
Do Util 的寫法
Function summarion(ByVal count As Integer) As Double
Dim i As Integer
Dim loopCount As Integer
Dim total As Double
i = 2
Do Until loopCount >= count
total = total + Sqr(-1 + i)
i = i + 2
loopCount = loopCount + 1
Loop
summarion = total
End Function
2007-05-05 02:00:44 補充:
答新田美香 你的方法很直接,但是有一要點注意要計 sum = 1+開方3+開方5+開方7+開方9count 是 10, 不是 5.如果要計 sum = 1+開方4+開方7+開方10+開方13count 就是 15所以 count 應該是 function 內的變數, 不應是 funtion argument
2007-05-05 02:05:59 補充:
可以改成Function summarion(ByVal count As Integer, ByVal stepx As Integer) As DoubleFor i = 1 To count * stepx' code herenextEnd function