无法显示索引和数组总和

时间:2019-09-17 09:04:40

标签: c#

我有作业。我必须制作一个可以输入带有值的数组长度的程序。单击“处理”按钮后,程序将输出索引和值以及数组中结果的总和和平均值。

https://www.mrexcel.com/forum/excel-questions/1109034-update-chart-sourcedata-dynamic-range-2.html?s=3e947d6afe969e2cbebc4fb73b165bda

我被卡住了,无法在处理按钮下面的多个文本框中打印索引和值。

我希望输出看起来像这样:

codeArray

这是到目前为止我成功编写的代码:

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);
        }
    }
}

请帮帮我。

2 个答案:

答案 0 :(得分:0)

这是您的代码(boxAvg为平均值)

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-1] = Convert.ToInt32(Interaction.InputBox(message="enter the value of array number "+i));
                sum += varray[i-1];
            }
            //Refer your list box here to add newly added values to the list
            boxSum.Text = Convert.ToString(sum);
            boxAvg.Text = Convert.ToString(sum / count);  //calculate the average
        }
    }
}

答案 1 :(得分:0)

鉴于该数组已具有数据:

private void Display()
{
    var columnHeader1 = "Index    Value\n";
    multilineTextBox1.Text = 
        columnHeader1 +
        string.Join("\n", vArray.Select((x,i)=> $"{i+1,5}    {x,5}"));


    boxSum.Text = vArray.Sum(); 
    boxAvg.Text = vArray.Average();


    var columnHeader2 = "Index    Value    Histogram\n";
    multilineTextBox2.Text = 
        columnHeader2 + 
        string.Join("\n", vArray.Select((x,i)=> $"{i+1,5}    {x,5}    {new String('*', x)}"));
}

Console demo