vb.net 如何生成斐波那契数列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20857943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:18:12  来源:igfitidea点击:

How to generate Fibonacci series?

vb.net

提问by usminuru

I am a beginner for programming and dealing with concept of visual basic 2010 to generate the following Fibonacci series output that will get the next number by adding two consecutive numbers

我是编程和处理visual basic 2010概念的初学者,以生成以下斐波那契系列输出,该输出将通过添加两个连续数字来获得下一个数字

I have tried that by creating variables like bellow

我已经尝试通过创建像波纹管这样的变量

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

回答by Orville Patterson

This should work

这应该工作

 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

回答by Nagaraj S

Private Sub Command1_Click()
Dim x, g, n, i, sum As Integer
n = Val(Text1.Text)
x = 0
y = 1
Print x
Print y
For i = 3 To n
sum = x + y
Print sum
x = y
y = sum
y = sum
Next i
End Sub

image

图片

回答by Sebastián Olate Bustamante

Here the solution:

这里的解决方案:

Dim num1 As Integer = 1
Dim num2 As Integer = 1
Dim aux As Integer

Console.Write(num1)
Console.Write(", ")
Console.Write(num2)
Console.Write(", ")

While num1 < 100
   aux = num1 + num2
   Console.Write(aux)
   Console.Write(", ")
   num1 = num2
   num2 = aux
End While

This print Fibonacci series till number 100

这个打印斐波那契数列直到数字 100

I hope this help you!

我希望这对你有帮助!