1.純迴圈
Protected Sub Button1_Click(........) Handle Button1.Click
Dim i As Integer, Ans As Long = 0
Const n As Integer = 10
For i = 1 To n
Ans += i ^ i
Next
MsgBox(Ans)
End Sub
2.函數
Protected Sub Button1_Click(........) Handle Button1.Click
Const n As Integer = 10
MsgBox Compute(n)
End Sub
Private Function Compute(ByVal n As Integer) As Long
Dim i As Integer, Ans As Long = 0
For i = 1 To n
Ans += i ^ i
Next
Return Ans
End Function
3.遞迴函數
Protected Sub Button1_Click(........) Handle Button1.Click
Const n As Integer = 10
MsgBox Compute(n)
End Sub
Private Function Compute(ByVal n As Integer) As Long
If n < 1 Then Return 0 Else Return Compute(n - 1) + n ^ n
End Function
Compute(3)
= Compute(2) + 3 ^ 3
= Compute(1) + 2 ^ 2 + 3 ^ 3
= Compute(0) + 1 ^ 1 + 2 ^ 2 + 3 ^ 3
= 0 + 1 ^ 1 + 2 ^ 2 + 3 ^ 3
--
▃▅▇▆▄ ▆▂▃ `
逝去感情如何能留住,半點癡情遺留殊不易,██▅▇▄▃ ▇▃▂" .
█████████▃i ▁▄▇
更多悽悽慘慘的遭遇…………██▆▃ █▅▆▃ˍ▄*
◢ ▂█▄▇▅▂▌.
我不知道,王~八~蛋~~! ▂▆███ █▄▃ 。fumizuki。Check。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.194.100.4