数组---显示全0的列表。怎么修

时间:2019-02-25 03:10:28

标签: c# winforms

我必须对一个数组进行编码,该数组最多显示输入到计算器中的20个数字。由于某种原因,我的代码显示零20次。我似乎无法弄清楚我做错了什么,我觉得这很明显。有人可以帮忙吗?我真的不想作弊,我只是想指出正确的方向。

 public partial class Form1 : Form
{

    int Count = 0;
    decimal Total = 0m;

    public Form1()
    {
        InitializeComponent();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        decimal Score = Convert.ToDecimal(txtScore.Text);

        Total += Score;
        Count++;

        decimal Average = Total / Count;

        txtTotal.Text = Total.ToString();
        txtCount.Text = Count.ToString();
        txtAverage.Text = Average.ToString();




    }

    private void btnClear_Click(object sender, EventArgs e)
    {

        int Count = 0;
        decimal Total = 0m;
        decimal Average = 0m;
        decimal Score = 0m;

        txtScore.Text = Score.ToString();
        txtAverage.Text = Average.ToString();
        txtTotal.Text = Total.ToString();
        txtCount.Text = Count.ToString();





        txtScore.Focus();

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnDisplay_Click(object sender, EventArgs e)
    {

        decimal[] scores = new decimal[20];

        MessageBox.Show("The scores entered are " + Environment.NewLine +
            scores[0] + Environment.NewLine +
            scores[1] + Environment.NewLine +
            scores[2] + Environment.NewLine +
            scores[3] + Environment.NewLine +
            scores[4] + Environment.NewLine +
            scores[5] + Environment.NewLine +
            scores[6] + Environment.NewLine +
            scores[7] + Environment.NewLine +
            scores[8] + Environment.NewLine +
            scores[9] + Environment.NewLine +
            scores[10] + Environment.NewLine +
            scores[11] + Environment.NewLine +
            scores[12] + Environment.NewLine +
            scores[13] + Environment.NewLine +
            scores[14] + Environment.NewLine +
            scores[15] + Environment.NewLine +
            scores[16] + Environment.NewLine +
            scores[17] + Environment.NewLine +
            scores[18] + Environment.NewLine +
            scores[19] + Environment.NewLine, "Scores List");



    }
}

}

2 个答案:

答案 0 :(得分:1)

每当单击btnDisplay时,您将创建一个新的空小数数组。这就是为什么它总是显示20 0的原因。

您需要更改数组的范围,以便可以在您所有的类中访问它。这意味着您随后将在btnDisplay_click中使用该变量。需要将所需的数字插入btnAdd_Click上的数组中。

 public partial class Form1 : Form
{

    int Count = 0;
    decimal Total = 0m;
    decimal[] scores = new decimal[20];

    public Form1()
    {
        InitializeComponent();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        decimal Score = Convert.ToDecimal(txtScore.Text);

        //Add the score to the array
        scores[Count] = Score;

        Total += Score;
        Count++;

        decimal Average = Total / Count;

        txtTotal.Text = Total.ToString();
        txtCount.Text = Count.ToString();
        txtAverage.Text = Average.ToString();

    }

    private void btnClear_Click(object sender, EventArgs e)
    {

        int Count = 0;
        decimal Total = 0m;
        decimal Average = 0m;
        decimal Score = 0m;

        txtScore.Text = Score.ToString();
        txtAverage.Text = Average.ToString();
        txtTotal.Text = Total.ToString();
        txtCount.Text = Count.ToString();


        txtScore.Focus();

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnDisplay_Click(object sender, EventArgs e)
    {

        MessageBox.Show("The scores entered are " + Environment.NewLine +
            scores[0] + Environment.NewLine +
            scores[1] + Environment.NewLine +
            scores[2] + Environment.NewLine +
            scores[3] + Environment.NewLine +
            scores[4] + Environment.NewLine +
            scores[5] + Environment.NewLine +
            scores[6] + Environment.NewLine +
            scores[7] + Environment.NewLine +
            scores[8] + Environment.NewLine +
            scores[9] + Environment.NewLine +
            scores[10] + Environment.NewLine +
            scores[11] + Environment.NewLine +
            scores[12] + Environment.NewLine +
            scores[13] + Environment.NewLine +
            scores[14] + Environment.NewLine +
            scores[15] + Environment.NewLine +
            scores[16] + Environment.NewLine +
            scores[17] + Environment.NewLine +
            scores[18] + Environment.NewLine +
            scores[19] + Environment.NewLine, "Scores List");

    }

}

还需要在btnClear_Click上清除数组。

答案 1 :(得分:0)

btnDisplay_Click中,您创建了scores数组,但未在其中放置任何内容。然后显示其内容。它显示出预期的20个零。

如果要在scores中填写字段,则必须将btnAdd_Click设置为字段。