数组不';t在消息框中显示-C#

数组不';t在消息框中显示-C#,c#,arrays,output,C#,Arrays,Output,我试图在一个数组中显示5个分数,但不幸的是,我在消息框中得到的结果都是0 任何帮助都将不胜感激 public partial class Form1 : Form { private int[] scoresArray = new int[5]; private int scoreTotal = 0; private int scoreCount = 0; public Form1() { InitializeComponent();

我试图在一个数组中显示5个分数,但不幸的是,我在消息框中得到的结果都是0

任何帮助都将不胜感激

public partial class Form1 : Form
{
    private int[] scoresArray = new int[5];
    private int scoreTotal = 0;
    private int scoreCount = 0;

    public Form1()
    {
        InitializeComponent();
    }
单击add(添加)按钮时,分数最多存储在数组中5次

    private void btnAdd_Click(object sender, EventArgs e)
    {

        try
        {
            if (txtScore.Text == "")
            {
                MessageBox.Show("Score is required", "Entry Error");
            }

            else
            {
                int score = Convert.ToInt32(txtScore.Text);
                decimal average = 0;

                if (score >= 0 && score <= 100)
                {
                    if (scoreCount != 4)
                    {
                        scoreTotal += score;
                        scoresArray[scoreCount] = score;
                        scoreCount++;
                        average = scoreTotal / scoreCount;
                    }
                    else
                    {
                        MessageBox.Show("Array is full");
                    }

                    txtScoreTotal.Text = scoreTotal.ToString();
                    txtScoreCount.Text = (scoreCount + 1).ToString();
                    txtAverage.Text = average.ToString();
                }

                else
                {
                    MessageBox.Show("Score must be greater than 0 and less than or equal to 100.", "Entry Error");
                }
            }
        }

        catch (FormatException)
        {
            MessageBox.Show("Please enter a valid number for the Score field.", "Entry Error");
        }

        txtScore.Focus();
    }

    private void btnDisplayScores_Click(object sender, EventArgs e)
    {
        string message = "";

        for (int i = 0; i < scoresArray.Length; i++)
        {
            message = scoresArray[i].ToString() + "\n";
        }

        MessageBox.Show(message, "Scores");
    }
private void btnAdd\u单击(对象发送者,事件参数e)
{
尝试
{
如果(txtScore.Text==“”)
{
MessageBox.Show(“需要分数”,“输入错误”);
}
其他的
{
int score=Convert.ToInt32(txtScore.Text);
十进制平均数=0;
如果(score>=0&&score您在这个循环中一直覆盖
消息

for (int i = 0; i < scoresArray.Length; i++)
{
    message = scoresArray[i].ToString() + "\n";
}
for(int i=0;i
因此,它只会显示最后一个值。您可能希望将其附加到:

for (int i = 0; i < scoresArray.Length; i++)
{
    message += scoresArray[i].ToString() + "\n";
}
for(int i=0;i
您在这个循环中不断覆盖
消息

for (int i = 0; i < scoresArray.Length; i++)
{
    message = scoresArray[i].ToString() + "\n";
}
for(int i=0;i
因此,它只会显示最后一个值。您可能希望将其附加到:

for (int i = 0; i < scoresArray.Length; i++)
{
    message += scoresArray[i].ToString() + "\n";
}
for(int i=0;i
谢谢,它起作用了,但现在当你不输入分数时,它会显示额外的零。@Benyamin:好吧,你有一个长度为5的数组。不管你是否更新所有5个值,它总是长度为5。0是整数的默认值。如果集合的长度需要是动态的,你可以尝试类似于
列表的方法ode>而不是数组。这样,例如,如果只输入了3个分数,则您可以只向集合中动态添加3个元素。感谢它的工作,但现在当您不输入分数时,它会显示额外的零。@Benyamin:嗯,您有一个长度为5的数组。无论您是否更新所有5个值,它的长度始终为5。0是整数的默认值。如果集合的长度需要是动态的,您可以尝试使用
列表
而不是数组。这样,例如,如果只输入了3个分数,则您可以只向集合动态添加3个元素。