我再一次得到了一个无法完成的阵列!我得到它填充和我输入的信息,但我希望它也给我一个整个quizgrade的平均值。基本上每当我向数组添加一个测验成绩时,我希望它将它们全部加在一起然后给我平均值。我出于好的想法,无法弄清楚这一部分。请有人帮忙吗。所有帮助表示赞赏!感谢
给出一点历史: 此代码将人员命名为其编号和等级,将其全部添加到数组中,然后将其显示在列表框中。但是,我需要它来平均我放入阵列的等级,但我尝试过的任何作品都没有。以下是代码,也许有人可能会提供帮助。
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);
}
}
}
答案 0 :(得分:1)
您可以使用其中一个Enumerable.Average(TSource)函数。
答案 1 :(得分:0)
你可以做一个Foreach循环并将它们全部加在一起然后除以条目数。如下。
int total = 0;
foreach(int a in quizGrade)
{
total = total + a;
}
然后除以数组的总数。
total = total / numberOfEntries;
这样的事情。我希望这有帮助!
答案 2 :(得分:0)
首先,让我们通过将成绩记录捆绑在一起来摆脱大部分复杂性。
public class StudentQuizGrade {
public string StudentName {get; set;}
public string StudentNumber {get; set;}
public int QuizGrade {get; set;}
}
我们在重复数组大小时删除了“不要重复自己”的错误。这让我们可以编写你已经拥有的内容
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
StudentQuizGrade [] quizGrades = new StudentQuizGrade [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");
foreach (StudentQuizGrade quizGrade in quizGrades)
{
listBox1.Items.Add(quizGrade.StudentName + "\t" + quizGrade.StudentNumber + "\t\t" + quizGrade.QuizGrade);
}
}
public void addToArray()
{
Array.Resize(ref quizGrades, quizGrades.Count + 1);
quizGrade[quizGrades.Count- 1] = new StudentQuizGrade() {
QuizGrade = Convert.ToInt32(txtQuizGrade.Text);
StudentName = txtStudentName.Text;
StudentNumber = 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(quizGrades, 0,quizGrades.Length);
}
}
接下来,我们将清理重复更改阵列时发生的事情。我们将通过在我们更改数组中的内容时添加一个事件来实现此目的。需要通知的事项可以添加到QuizGradesChanged
,更改quizGrades
的内容应该调用QuizGradesChanged()
。我们还将使用新方法clearAddInputs()
重构清除添加输入,并删除一些不必要的方法。
public delegate void EventHandler();
public partial class Form1 : Form
{
public event EventHandler QuizGradesChanged;
public Form1()
{
InitializeComponent();
QuizGradesChanged += addArrayElementsToListbox;
}
StudentQuizGrade [] quizGrades = new StudentQuizGrade [0];
private void button1_Click(object sender, EventArgs e)
{
addToArray();
clearAddInputs();
}
private void clearAddInputs()
{
txtStudentName.Clear();
txtStudentNumber.Clear();
txtQuizGrade.Clear();
}
public void addArrayElementsToListbox()
{
listBox1.Items.Clear();
listBox1.Items.Add("Student Name \t Student Number \t Quiz Grade");
foreach (StudentQuizGrade quizGrade in quizGrades)
{
listBox1.Items.Add(quizGrade.StudentName + "\t" + quizGrade.StudentNumber + "\t\t" + quizGrade.QuizGrade);
}
}
public void addToArray()
{
Array.Resize(ref quizGrades, quizGrades.Count + 1);
quizGrade[quizGrades.Count- 1] = new StudentQuizGrade() {
QuizGrade = Convert.ToInt32(txtQuizGrade.Text);
StudentName = txtStudentName.Text;
StudentNumber = txtStudentNumber.Text;
}
QuizGradesChanged();
}
private void ClearButton_Click(object sender, EventArgs e)
{
clearAddInputs();
Array.Clear(quizGrades, 0,quizGrades.Length);
QuizGradesChanged();
}
}
最后,我们可以添加测验成绩的平均值。我们将有一个方法recalculateAverage()
重新计算平均值,并将其显示在txtAverage
控件中。
public Form1()
{
InitializeComponent();
QuizGradesChanged += addArrayElementsToListbox;
QuizGradesChanged += recalculateAverage
}
public void recalculateAverage() {
if (quizGrades.Count > 0) {
txtAverage.Text = quizGrades.Select(x => (decimal) x.QuizGrade).Average().ToString();
} else {
txtAverage.Text = "No grades to average";
}
}
编辑:数组......呃
中可能存在错误
private void ClearButton_Click(object sender, EventArgs e)
{
clearAddInputs();
Array.Clear(quizGrades, 0,quizGrades.Length);
QuizGradesChanged();
}
我更有信心这是正确的:
private void ClearButton_Click(object sender, EventArgs e)
{
clearAddInputs();
quizGrades = new StudentQuizGrade[0];
QuizGradesChanged();
}