返回斐波那契级数c#

返回斐波那契级数c#,c#,fibonacci,C#,Fibonacci,我需要创建一个返回斐波那契数列中第n个整数的方法,我编写(编辑)的代码不起作用,有人能在for循环部分指导我吗。我需要使用webform并将斐波那契数列返回到特定点 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; { public partial class

我需要创建一个返回斐波那契数列中第n个整数的方法,我编写(编辑)的代码不起作用,有人能在for循环部分指导我吗。我需要使用webform并将斐波那契数列返回到特定点

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

{
    public partial class Default : System.Web.UI.Page
    {
        int i, temp;

        public void Page_Load(object sender, EventArgs e)
        {

        }

        public int Fibonacci(int x)
        {
            if (x == 0)
            {
                return 1;
            }
            if (x == 1)
            {
                return 1;
            }
            else
            {
                return (Fibonacci(x - 2) + Fibonacci(x - 1));
            }

        }

        public void btSubmit_Click(object sender, EventArgs e)
        {
            // getting input from user
            int num = Convert.ToInt32(txtInput.Text);

            // logic for fibonacci series
            for (i = 0; i < num; i++)
            {
                lblResult.Text = Fibonacci(i).ToString();
            }

        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
{
公共部分类默认值:System.Web.UI.Page
{
int i,温度;
公共无效页面加载(对象发送方,事件参数e)
{
}
公共整数斐波那契(整数x)
{
如果(x==0)
{
返回1;
}
如果(x==1)
{
返回1;
}
其他的
{
收益率(斐波那契(x-2)+斐波那契(x-1));
}
}
公共无效btSubmit\u单击(对象发送者,事件参数e)
{
//从用户获取输入
int num=Convert.ToInt32(txtInput.Text);
//斐波那契级数的逻辑
对于(i=0;i
您的错误是覆盖文本,而不是添加到文本中。将其更改为
lblResult.Text+=Fibonacci(i).ToString()

但是请注意,将大量文本从循环附加到GUI元素是一种有问题的操作。读写GUI元素会产生大量开销。每个用户触发的事件只执行一次并不重要,但通过循环,您会很快注意到它

最好是在代码隐藏中构建序列,然后一次性显示它。我甚至编写了一个示例代码来展示这些问题:

using System;
using System.Windows.Forms;

namespace UIWriteOverhead
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int[] getNumbers(int upperLimit)
        {
            int[] ReturnValue = new int[upperLimit];

            for (int i = 0; i < ReturnValue.Length; i++)
                ReturnValue[i] = i;

            return ReturnValue;
        }

        void printWithBuffer(int[] Values)
        {
            textBox1.Text = "";
            string buffer = "";

            foreach (int Number in Values)
                buffer += Number.ToString() + Environment.NewLine;
            textBox1.Text = buffer;
        }

        void printDirectly(int[] Values){
            textBox1.Text = "";

            foreach (int Number in Values)
                textBox1.Text += Number.ToString() + Environment.NewLine;
        }

        private void btnPrintBuffer_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Generating Numbers");
            int[] temp = getNumbers(10000);
            MessageBox.Show("Printing with buffer");
            printWithBuffer(temp);
            MessageBox.Show("Printing done");
        }

        private void btnPrintDirect_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Generating Numbers");
            int[] temp = getNumbers(1000);
            MessageBox.Show("Printing directly");
            printDirectly(temp);
            MessageBox.Show("Printing done");
        }
    }
}
使用系统;
使用System.Windows.Forms;
命名空间UIWriteOverhead
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
int[]getNumbers(int上限)
{
int[]ReturnValue=新int[上限];
for(int i=0;i

正如另一位评论者所提到的,你可能也希望进行某种形式的多任务处理。事实上,我的第一次多任务学习经历是Fibbonacci/素数检查。这些都是很好的学习例子。

你的错误是覆盖了文本,而不是添加到文本中。将其更改为
lblResult.Text+=Fibonacci(i).ToString()

但是请注意,将大量文本从循环附加到GUI元素是一种有问题的操作。读写GUI元素会产生大量开销。每个用户触发的事件只执行一次并不重要,但通过循环,您会很快注意到它

最好是在代码隐藏中构建序列,然后一次性显示它。我甚至编写了一个示例代码来展示这些问题:

using System;
using System.Windows.Forms;

namespace UIWriteOverhead
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int[] getNumbers(int upperLimit)
        {
            int[] ReturnValue = new int[upperLimit];

            for (int i = 0; i < ReturnValue.Length; i++)
                ReturnValue[i] = i;

            return ReturnValue;
        }

        void printWithBuffer(int[] Values)
        {
            textBox1.Text = "";
            string buffer = "";

            foreach (int Number in Values)
                buffer += Number.ToString() + Environment.NewLine;
            textBox1.Text = buffer;
        }

        void printDirectly(int[] Values){
            textBox1.Text = "";

            foreach (int Number in Values)
                textBox1.Text += Number.ToString() + Environment.NewLine;
        }

        private void btnPrintBuffer_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Generating Numbers");
            int[] temp = getNumbers(10000);
            MessageBox.Show("Printing with buffer");
            printWithBuffer(temp);
            MessageBox.Show("Printing done");
        }

        private void btnPrintDirect_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Generating Numbers");
            int[] temp = getNumbers(1000);
            MessageBox.Show("Printing directly");
            printDirectly(temp);
            MessageBox.Show("Printing done");
        }
    }
}
使用系统;
使用System.Windows.Forms;
命名空间UIWriteOverhead
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
int[]getNumbers(int上限)
{
int[]ReturnValue=新int[上限];
for(int i=0;i
正如另一位评论者所提到的,你可能也希望进行某种形式的多任务处理。事实上,我的第一次多任务学习经历是Fibbonacci/素数检查。他们是很好的学习例子。

Yo
GetNthFibonacci_Ite(7);
    public int Fibonacci(int n)
    {
        int a = 0;
        int b = 1;

        for (int i = 0; i < n; i++)
        {
            int temp = a;
            a = b;
            b = temp + b;
        }
        return a;
    }
lblResult.Text = Fibonacci(i).ToString();
lblResult.Text += Fibonacci(i).ToString();
   F(0) = 0,
   F(1) = 1,
   ...
   F(N) = F(N - 1) + F(N - 2)   
  using System.Linq;

  ... 

  //TODO: do you really want int as a return type? BigInteger seems to be a better choice 
  public static IEnumerable<int> Fibonacci() {
    int n_2 = 1; // your rules; or start Fibonacci from 1st, not 0th item
    int n_1 = 1;

    yield return n_2;             
    yield return n_1;

    while (true) {
      int n = n_2 + n_1;

      yield return n;

      n_2 = n_1;
      n_1 = n;
    }
  }
  lblResult.Text = string.Join(", ", Fibonacci().Take(num));        
  1, 1, 2, 3, 5, 8, 13
  // 8
  lblResult.Text = Fibonacci().ElementAt(5).ToString();