新手请教
用以实现FIBONACCI数列的输出Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Fib() As Long = New Long(49) {}
Dim i As Integer = 0
Fib(0) = 1
Fib(1) = 1
txbFib.Text = "1,1"
While i >= 2 And i <= 49
Fib(i) = Fib(i - 2) + Fib(i - 1)
i += 1
txbFib.Text &= ("," & Fib(i))
End While
End Sub
输出怎么只有 “1,1”呢? --------------------编程问答-------------------- 在线等待。。 --------------------编程问答-------------------- 你前面定义了Dim i As Integer = 0,后面的循环While i >= 2 And i <= 49
根本就不会进入了嘛 --------------------编程问答-------------------- 那要怎么修改呢?
--------------------编程问答-------------------- Dim i As Integer = 2
--------------------编程问答-------------------- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Fib() As Long = New Long(49) {}
Dim i As Integer = 2
Fib(0) = 1
Fib(1) = 1
txbFib.Text = "1,1"
While i >= 2 And i <= 49
Fib(i) = Fib(i - 2) + Fib(i - 1)
txbFib.Text &= ("," & Fib(i))
i += 1
End While
End Sub
--------------------编程问答-------------------- Dim Fib() As Long = New Long(49) {}
Fib(0) = 1
Fib(1) = 1
txbFib.Text = "1,1"
for i as integer=2 to 49
Fib(i) = Fib(i - 2) + Fib(i - 1)
txbFib.Text= txbFib.Text & "," & Fib(i)
next
End Sub
--------------------编程问答-------------------- 谢谢您哈。
为什么
txbFib.Text &= ("," & Fib(i))
i += 1
要交换位置才行... --------------------编程问答-------------------- 比如你定义了一个数组,有10个元素的,按照你写的顺序是先i += 1,再txbFib.Text &= ("," & Fib(i))
那么是不是在给txtFib.Text赋值的时候都是用自增后的i了,不就会超出索引咯,因为最后一次循环你是用
Fib(11)赋值的了 --------------------编程问答-------------------- 恩 就是超出索引
但是不知道为什么 中断后 还能运行出来1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
第一次循环运行到i+=1
接下来i=3 FIb(3)没法运行啊
--------------------编程问答-------------------- 是运行到Fib(50)的时候报错的,因为超出索引,至于为什么中断后能出结果,后面是0,因为在默认情况下,
该数组元素全是0.你可以
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Fib() As Long = New Long(49) {}
txtFib.Text = Fib(3)
End Sub
这样不会报错。
--------------------编程问答-------------------- 果然..呵呵
你真的是我的救命恩人。
感谢
祝你幸福。 --------------------编程问答--------------------
LZ太夸張了點吧
补充:.NET技术 , VB.NET