【发布时间】:2020-01-18 02:52:06
【问题描述】:
我有作业。我必须制作一个程序,它可以输入数组的长度和其中的值。单击“处理”按钮后,程序将输出索引和值以及数组中的结果总和和平均值。
我卡住了,无法将索引和值打印到进程按钮下方的多个文本框中。
我希望输出如下所示:
这是我目前成功编写的代码:
namespace ArrayProcess
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void process_Click(object sender, EventArgs e)
{
int sum = 0;
string ind = "index";
string message;
int count = Convert.ToInt32(inputArray.Text);
int[] varray = new int[count];
for (int i=1; i <= count; i++)
{
varray[i] = Convert.ToInt32(Interaction.InputBox(message="enter the value of array number "+i));
sum += varray[i];
}
boxSum.Text = Convert.ToString(sum);
}
}
}
请帮帮我。
【问题讨论】:
-
数组索引是从零开始的.. 即使显示索引 +1,您也应该从 0 开始循环。
-
并将行添加到多行文本框 = > How to add a line to a multiline TextBox?
-
for (int i = 0; i < varray.Length; i++) {...} -
数组变量应该在 Form1 中,而不是
process_Click的本地,因为表单中的许多元素都希望能够访问输入/数组。
标签: c#