VB两道调用题,请教!

来源:百度知道 编辑:UC知道 时间:2024/09/22 01:06:17
这道题是这样的,编写求Fibonacci(菲邦纳契)数列第K项的递归过程.当向文本框输入一个正整数N并单击按钮时,调用次递归过程在窗体上显示数列的前N项.
Fibonacci(菲邦纳契)数列就是前两个数都是1,后面的数都是前2个数的和.
比如:1 1 2 3 5 8 13 21..... 就是一个Fibonacci(菲邦纳契)数列
我的代码如下:
Function s(k As Integer) As Integer
Dim f1, f2, f3, i As Integer
Select Case k
Case 1
Print 1
Case 2
Print 1, 1,
Case Else
f1 = 1: f2 = 1
For i = 3 To k
f3 = f1 + f2
Print f3,
f1 = f2: f2 = f3
Next i
End Select
End Function

Private Sub Command1_Click()
Dim n As Integer
n = Int(t1.Text)
Print s(n)
End Sub
这段代码显示出来的数不对,麻烦高手给看下哪的错吧.

做简单的修改就行。
Function s(k As Integer) As Integer
Dim f1, f2, f3, i As Integer
Select Case k
Case 1
Print 1
Case 2
Print 1; 1;'逗号改成分号
Case Else
f1 = 1: f2 = 1
Print 1; 1;'加上一句
For i = 3 To k
f3 = f1 + f2
Print f3; '逗号改成分号
f1 = f2: f2 = f3
Next i
End Select
End Function

Private Sub Command1_Click()
Dim n As Integer
n = Int(t1.Text)
s(n) 'Print 去掉
End Sub

'******看看如何,改了两个地方
Function s(k As Integer) As Integer
Dim f1, f2, f3, i As Integer
Select Case k
Case 1
Print 1
Case 2
Print 1, 1,
Case Else
f1 = 1: f2 = 1
Print f1
Print f2
For i = 3 To k
f3 = f1 + f2
Print f3
f1 = f2: f2 = f3
Next i
End Select
End Function

Private Sub Command1_Click()
If IsNumeric(t1.Text) = True Then
Form1.Cls
Dim n As Integer
n