我又一次得到了一个我无法完成的数组!我得到它来填充我输入的信息,但我希望它也能给我整个测验成绩的平均值。基本上每次我向数组添加测验成绩时,我都希望它把它们加在一起,然后给我平均值。我没有好的想法,无法弄清楚这部分。有人可以帮忙吗。感谢所有帮助!谢谢
给一点历史:这段代码需要一个人的名字,他们的号码和他们的等级,将它们全部添加到一个数组中,然后将它显示在一个列表框中。但是,我也需要它来平均我放入数组中的成绩,但我没有尝试过任何工作。以下是代码,也许有人可以提供帮助。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[] quizGrade = new int[1];
string[] studentName = new string[1];
string[] studentNumber = new string[1];
int numberOfEntries = 0;
private void button1_Click(object sender, EventArgs e)
{
addToArray();
txtStudentName.Clear();
txtStudentNumber.Clear();
txtQuizGrade.Clear();
addArrayElementsToListbox();
}
public void addArrayElementsToListbox()
{
listBox1.Items.Clear();
listBox1.Items.Add("Student Name \t Student Number \t Quiz Grade");
for (int i = 0; i < studentName.Length; i++)
{
listBox1.Items.Add(studentName[i] + "\t" + studentNumber[i] + "\t\t" + quizGrade[i]);
}
}
public void addToArray()
{
if (numberOfEntries == 0)
{
quizGrade[0] = Convert.ToInt32(txtQuizGrade.Text);
studentName[0] = txtStudentName.Text;
studentNumber[0] = txtStudentNumber.Text;
numberOfEntries += 1;
}
else
{
numberOfEntries += 1;
Array.Resize(ref quizGrade, numberOfEntries);
Array.Resize(ref studentName, numberOfEntries);
Array.Resize(ref studentNumber, numberOfEntries);
quizGrade[numberOfEntries - 1] = Convert.ToInt32(txtQuizGrade.Text);
studentName[numberOfEntries - 1] = txtStudentName.Text;
studentNumber[numberOfEntries - 1] = txtStudentNumber.Text;
}
}
public void txtAverage_TextChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void ClearButton_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
txtStudentName.Clear();
txtStudentNumber.Clear();
txtQuizGrade.Clear();
Array.Clear(studentName, 0,studentName.Length);
Array.Clear(studentNumber, 0,studentNumber.Length);
Array.Clear(quizGrade, 0,quizGrade.Length);
}
}
}