【发布时间】:2014-05-27 23:12:29
【问题描述】:
我正在制作一个运行斐波那契数列的程序。我创建了 2 个数组。
第一个数组仅包含 0, 1(数组名称:- int[] arr)
第二个数组保存其他值,例如:1、2、3、5....等(数组名称:- int[] numbers)
我正在使用 while 循环来获取 febonacci 系列并将其存储在称为 int[] 数字的第二个数组中。
使用 while 循环获取值后,我使用
连接两个数组int[] final = arr.Concat(number).ToArray();
最后,我使用 foreach 循环将 febonacci 系列添加到列表框中。
我遇到的问题是,我无法连接两个数组。我试图在 while 循环的顶部分配数字数组。这样数字变量就可以在 while 循环之外访问。但我遇到了一个错误。
请看下面的代码:
private void button1_Click(object sender, EventArgs e)
{
int x = 0;
int y = 1;
int z = 0;
if (!String.IsNullOrEmpty(q1input.Text))
{
int value;
if (int.TryParse(q1input.Text, out value))
{
int[] arr = {x, y };
while (z < value)
{
z = x + y;
int[] number = {z};
x = y;
y = z;
}
int[] final = arr.Concat(number).ToArray();
foreach (int num in final)
{
q2listbox.Items.Add(num);
}
}
else
{
MessageBox.Show("It is not a numeric value");
}
}
else
{
MessageBox.Show("Invalid Input");
}
}
【问题讨论】:
-
这是你的作业吗?
-
你能解释一下
java和c++问题的标签吗? -
抱歉打错了标签。我是stackoverflow的新手。我想如果标记更多,我会很快得到帮助。
-
错了,如果你标记正确,你会得到帮助
-
好的。这不是家庭作业。只是通过一些互联网任务练习 C#。