用vb输出斐波那契数列前20项的所有偶数

编辑工程框怎么放也要怎么都没有前面的编辑框啊,怎么画的啊
2025-04-10 22:00:53
推荐回答(4个)
回答1:

Public Class Form1

    Dim arraa%(19)

    Dim i%, j%

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



        arraa(0) = 1

        arraa(1) = 1

        arraa(2) = 1

        Label2.Text = "1  1  1"

        For i = 3 To 19

            arraa(i) = arraa(i - 1) + arraa(i - 2) + arraa(i - 3)

            Label2.Text = Label2.Text & " " & arraa(i)

        Next

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        For i = 3 To 19

        

                If  arraa(i) Mod 2 = 0 Then Label4.Text = Label4.Text & "          " & arraa(i)

        Next

    End Sub

End Class

把图片上面的 数字改动 质数改成偶数就可以了

 貌似有一点 没有偶数 呢

回答2:

'百度 tonkeys 原创
Private Sub Command1_Click()
    Text1.Text = Fibonacci2(20) '调用函数返回前20项Fibonacci数的偶数,在文件框中输出结果
End Sub
'返回前N项Fibonacci数的偶数
Public Function Fibonacci2(N As Integer) As String
    Dim a As Integer '前二个数
    Dim b As Integer '前一个数
    Dim c As Integer '当前数
    Dim i As Integer '循环标记
    a = 1   '初始化起始1
    For i = 1 To N
        c = a + b   '当前数=前两数之和
        a = b       '为下一个数作准备
        b = c       '为下一个数作准备
        If c Mod 2 = 0 Then '仅取偶数
            rtnStr = rtnStr & c & ", "
        End If
    Next
    Fibonacci2 = rtnStr
End Function

回答3:

Private Sub Form_Click()
Dim d(100000), i As Long
d(0) = 0
d(1) = 1
For i = 2 To 20
d(i) = d(i - 1) + d(i - 2)
Next i
For i = 0 To 20
Print d(i) & Space(5);
If i Mod 5 = 0 Then Print
Next i
Print "偶数"
For i = 0 To 20
If d(i) Mod 2 = 0 Then Print d(i);
Next i

End Sub

回答4:

你要VB6还是VB.NET?