斐波那契数列第1项是0,第二项是1,以后各项都是前两的和,VB,求第N项的值 请帮修改,谢谢呀! Function

2024-11-30 11:01:47
推荐回答(3个)
回答1:

这样?

Function fun(x As Integer) As Integer
Dim a As Integer, b As Integer, c As Integer, n As Integer
a = 1
b = 1
c = 0
For n = 3 To x
c = a + b
a = b
b = c
Next
Return c
End Function

Private Sub Form_Click()
Dim n As Integer
n = InputBox("")
Print fun(n)
End Sub

回答2:

Function fun(x As Integer) As Integer
a = 0
b = 1
For n = 3 To x
c = a + b
a = b
b = c
Next n
fun = c
End Function

Private Sub Form_Click()
Dim n As Integer
n = Val(InputBox(""))
Print fun(n)

End Sub

回答3:

你这个是正确的!
n = 2
可以不要,放在哪里也不影响结果!