斐波那契序列Visual Basic

时间:2019-01-10 02:27:47

标签: vb.net visual-studio visual-studio-2015

我对正在处理的另一个Visual Basic任务有一个快速的问题。我拥有所有代码,到目前为止一切进展顺利。该应用程序旨在在列表框中显示前100个斐波那契数字,将之前显示的两个数字相加即可循环获得下一个。唯一的问题是,当我按下按钮以显示代码时,循环继续进行,而不仅仅是在100个数字处停止。我哪里出错了?

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    Dim dblA As Double = 0
    Dim dblB As Double = 1
    Dim dblC As Double
    Dim intCounter As Integer

    lstSequence.Items.Add(dblA.ToString)
    lstSequence.Items.Add(dblB.ToString)

    For intCounter = 1 To 100
        dblC = dblA + dblB
        dblA = dblB
        dblB = dblC
        lstSequence.Items.Add(dblC.ToString)
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

我刚刚尝试过。很好。

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim a As Integer = 0
        Dim b As Integer = 1
        Dim fib As Integer
        Dim userinput, i As Integer
        userinput = InputBox("how many interations?")
        i = userinput
        ListView1.Items.Add(1)
        Do
            fib = a + b
            a = b
            b = fib
            ListView1.Items.Add(fib)
            i = i + 1
        Loop While fib < i

    End Sub
End Class

enter image description here

enter image description here