VB.net中的Fibonacci序列使用循环

请你帮我看一下前10个斐波那契数字。我的代码显示以下结果:1,2,3,5,8,13,21,34,55,我需要它还显示前两个Fibonacci数字(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
在专业编程中你需要使用Fibonacci序列吗?     
已邀请:
只需添加
Label1.Text = Label1.Text + a.ToString & ControlChars.NewLine
Label1.Text = Label1.Text + b.ToString & ControlChars.NewLine
Do ... while
之前。 对于与Fib​​onacci数字相关的应用,请参阅: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 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
    

要回复问题请先登录注册