夥計們我有一個關於斐波那契的問題..我如何得到的斐波那契數列也將結束在用戶輸入...例如,如果我把21的輸出必須是0 1 1 2 3 5 8 13 21如何獲得斐波納契在c#
這是我的代碼
static void Main(string[] args)
{
int input, first = 0, second = 1, third = 0;
Console.Write("Enter a number : ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("First {0} Fibonacci numbers {1} {2} ", input, first, second);
for (int i = 3; i <= input; i++)
{
third = first + second;
Console.Write("{0} ", third);
first = second;
second = third;
}
}
您將用戶輸入轉換爲'n',而不是'input'。如果你打算爲你的循環使用'input',那真的是你想要做的嗎? –