[请您帮我显示前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
在专业编程中的哪里需要使用斐波那契数列?
只需添加
Label1.Text = Label1.Text + a.ToString & ControlChars.NewLine
Label1.Text = Label1.Text + b.ToString & ControlChars.NewLine
Do ... while
之前。
有关与斐波那契数字关联的应用程序,请参见:Fibonacci: Applications
而不是计算下一个顺序号,然后将结果添加到输出中,请按相反的顺序进行:
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
Do
Label1.Text += a.ToString & ControlChars.NewLine
fib = a + b
a = b
b = fib
Loop While a <= 55
End Sub
以与您在代码中将前两个斐波那契数定义为0和1相同的方式,应在开头将它们放入标签字符串中(即不在循环中)。您可能还应该对已计算的斐波那契数的数量使用循环条件,而不要依赖于知道第10个数。
我从未在工作中使用斐波那契数,但是它们与朴素的递归语言一样是一项很好的学习练习,其中包含查找表,简单的迭代语言(如您的语言),使用黄金比例,矩阵形式.. 。
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer
Dim userinput, i As Integer
userinput = InputBox("how many")
i = userinput
ListView3.Items.Add(1)
Do
fib = a + b
a = b
b = fib
ListView3.Items.Add(fib)
i = i + 1
Loop While fib < i
End Sub
结束类
尝试此代码:
Dim arr As New ArrayList()
Console.Write("The Fibonacci Series is : ")
For i As Integer = 0 To 10
If i = 0 Or i = 1 Then
arr.Add(i)
Console.Write(arr(i).ToString() + ", ")
Else
arr.Add(arr(i - 2) + arr(i - 1))
If i = 10 Then
Console.Write(arr(i).ToString())
Else
Console.Write(arr(i).ToString() + ", ")
End If
End If
Next
Console.Read()
非常简单,只需使用一个按钮,就可以根据需要生成任意数量的序列。
Sub fibonacci()
mycount = Application.CountA(Range("A:A"))
e = mycount - 1
fib = 0
fib = Cells(e, 1).Value + Cells(e + 1, 1).Value
Cells(mycount + 1, 1).Value = fib
mycount = mycount + 1
End Sub
Dim n作为整数n = inputBox(“输入数字”)昏暗为整数Dim b作为整数昏暗我作为整数a = 0b = 1打印b对于I = 1至nc = a + b打印ca = bb = c下一个结束子
Dim a, b, c as integer
a=0
b=1
print a
print b
while c<(n-c)
c=a+b
print c
a=b
b=c
wend
print "This is Fibonacci Series"
End Sub