VB.net 中使用循环的斐波那契数列
请您帮我显示前 10 个斐波那契数列。我的代码显示以下结果:1、2、3、5、8、13、21、34、55,我还需要它显示前两个斐波那契数(0 和 1)。我该怎么做呢?
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer = 0
Do
fib = a + b
a = b
b = fib
Label1.Text = Label1.Text + fib.ToString & ControlChars.NewLine
Loop While fib < 55
End Sub
End Class
在专业编程中,哪些地方需要使用斐波那契数列?
Please could you help me with displaying the first 10 Fibonacci numbers. My code displays the following result: 1, 2, 3, 5, 8, 13, 21, 34, 55 and I need it to also display the first two Fibonacci numbers (0 and 1). How would I do that?
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer = 0
Do
fib = a + b
a = b
b = fib
Label1.Text = Label1.Text + fib.ToString & ControlChars.NewLine
Loop While fib < 55
End Sub
End Class
Where in professional programming would you need to use Fibonacci sequences?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
添加即可
只需在
Do ... while
之前。有关与斐波那契数列相关的应用程序,请参阅:斐波那契数列:应用程序
Just add
before the
Do ... while
.For applications linked to Fibonacci numbers see : Fibonacci: Applications
不要计算序列号中的下一个,然后将结果添加到输出中,而是以相反的顺序执行:
Instead of calculating the next in sequence number and then adding the results to the output, do it in reverse order:
就像您在代码中将前两个斐波那契数定义为 0 和 1 一样,您应该将它们放入开头的标签字符串中(即不在循环中)。您还应该对计算出的斐波那契数的数量使用循环条件,而不是依赖于知道第 10 个数是多少。
我从来没有在工作中使用过斐波那契数,但是它们是一个很好的学习练习,具有朴素的递归解决方案,一个带有查找表的简单迭代解决方案(如您的),使用黄金比例,矩阵形式......
In the same way that you have defined the first two fibonacci numbers in your code to be 0 and 1 you should put them into the label string at the beginning (i.e. not in the loop). You should also probably use a loop condition on the number of fibonacci numbers you've calculated instead of relying on knowing what the 10th one is.
I've never used Fibonacci numbers at work however they are quite a good learning exercise with the naive recursive soloution, one with a lookup table, a simple iterative soloution (like yours), using the golden ratio, the matrix form ...
结束课程
End Class
试试这个代码:
Try this code:
非常简单,只需使用一个按钮,您就可以生成任意数量的序列。
Pretty Symple, just using a button, and you can generate as many numbers of the sequence as you want.
子主函数()
Sub Main()