VB.net 中使用循环的斐波那契数列

发布于 2024-10-29 23:44:15 字数 525 浏览 7 评论 0原文

请您帮我显示前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

谎言月老 2024-11-05 23:44:15

添加即可

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

只需在 Do ... while 之前

。有关与斐波那契数列相关的应用程序,请参阅:斐波那契数列:应用程序

Just add

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

before the Do ... while.

For applications linked to Fibonacci numbers see : Fibonacci: Applications

大姐,你呐 2024-11-05 23:44:15

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

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

Instead of calculating the next in sequence number and then adding the results to the output, do it in reverse order:

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
万人眼中万个我 2024-11-05 23:44:15

就像您在代码中将前两个斐波那契数定义为 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 ...

‖放下 2024-11-05 23:44:15
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

结束课程

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

End Class

夜夜流光相皎洁 2024-11-05 23:44:15

试试这个代码:

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

Try this code:

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()
独留℉清风醉 2024-11-05 23:44:15

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

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

Pretty Symple, just using a button, and you can generate as many numbers of the sequence as you want.

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
她如夕阳 2024-11-05 23:44:15

子主函数()

    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

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
与风相奔跑 2024-11-05 23:44:15
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
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
穿越时光隧道 2024-11-05 23:44:15
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文