小编Doo*_*ard的帖子

仅使用正整数时返回负值

运行以下代码时,我最终获得负值.我只使用正数,所以我真的很困惑.我试图看看这个负数是否是返回码,但似乎没有任何代码将此作为数字返回.

  public static int fib(int n)
    {
        int a = 0;
        int b = 1;
        for (int i = 0; i < n; i++)
        {
            int temp = a;
            a = b;
            b = temp + b;
        }

        return b;
    }

    static void Main(string[] args)
    {
        int n = 0;
        bool Run = true;

        while (Run == true)
        {
            n = fib(n + 1);
            Console.WriteLine(n);
        }
    }
Run Code Online (Sandbox Code Playgroud)

以下是代码运行时的结果:

1
2
3
5
13
610
-121099088
1
Run Code Online (Sandbox Code Playgroud)

c# math

1
推荐指数
2
解决办法
1354
查看次数

标签 统计

c# ×1

math ×1