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

时间:2011-04-03 18:55:24

标签: 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序列?

9 个答案:

答案 0 :(得分:3)

添加

Label1.Text = Label1.Text + a.ToString & ControlChars.NewLine
Label1.Text = Label1.Text + b.ToString & ControlChars.NewLine

Do ... while

之前

对于与斐波纳契数字相关的应用,请参阅:Fibonacci: Applications

答案 1 :(得分:2)

不是计算序列号中的下一个,然后将结果添加到输出中,而是按相反的顺序执行:

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

答案 2 :(得分:1)

与您在代码中将前两个斐波纳契数定义为0和1的方式相同,您应该将它们放在开头的标签字符串中(即不在循环中)。您还应该对您计算的斐波那契数的数量使用循环条件,而不是依赖于知道第10个是什么。

我从来没有在工作中使用斐波那契数字,但它们是一个很好的学习练习,使用天真的递归求解,一个带有查找表,一个简单的迭代求解(就像你的),使用黄金比例,矩阵形式。 ..

答案 3 :(得分:1)

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

结束班

答案 4 :(得分:0)

试试这段代码:

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()

答案 5 :(得分:0)

非常简单,只需使用按钮,就可以根据需要生成任意数量的序列。

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

答案 6 :(得分:-1)

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

答案 7 :(得分:-1)

Dim n As integer 
n= inputBox("ENTER A NUMBER")
Dim a As integer 
Dim b As integer 
Dim I As integer 
a=0
b=1
Print b
for I= 1To n
c= a+b
Print c
a=b
b=c
Next
End Sub

答案 8 :(得分:-1)

Sub Main()

    Dim previousfibo As Integer = 0
    Dim currentfibo As Integer = 1
    Dim nextfibo As Integer
    previousfibo = 0
    currentfibo = 1
    Console.WriteLine(previousfibo)
    Console.WriteLine(currentfibo)
    For I = 1 To 9

        nextfibo = previousfibo + currentfibo
        Console.WriteLine(nextfibo)
        previousfibo = currentfibo
        currentfibo = nextfibo
    Next I
    Console.ReadLine()




End Sub