I have the same assignment someone else ask help with. The assignment calls for a test score calculator that calculates and displays the score total, score count, and average score. If the user clicks the Display scores button, the application sorts the scores and displays them in a message box. There is also a Clear button to have all entries removed.
My program is a complete mess, as it will not total the scores as it should, each iteration is counted as 2, and it will not calculate the average either. The code I have so far is below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestScoresCalc
{
public partial class frmScoreCalculator : Form
{
public frmScoreCalculator()
{
InitializeComponent();
}
List<int> scores = new List<int>();
private void btnAdd_Click(object sender, EventArgs e)
{
//List<int> scores = new List<int>();
scores.Add(20);
int sum = 0;
for (int i = 0; i < scores.Count; i++)
{
int score = scores[i];
sum += score;
}
if (txtScore.Text == "")
{
txtScore.Focus();
return;
}
else
{
int score = int.Parse(txtScore.Text);
int Total = 0;
int Average = 0;
Total = score;
Average = Total / scores.Count;
txtScoreTotal.Text = Total.ToString();
txtAverage.Text = Average.ToString("");
txtScoreCount.Text = scores.Count.ToString("");
txtScore.Text = "";
txtScore.Focus();
}
}
private void btnDisplayScores_Click(object sender, EventArgs e)
{
scores.Sort();
string scoresString = "List of all scores entered :\n\n";
foreach (int i in scores)
scoresString += i + "\n";
MessageBox.Show(scoresString, "Sorted Scores");
}
private void btnClearScores_Click(object sender, EventArgs e)
{
scores.Clear();
txtScoreTotal.Text = "0";
txtScoreCount.Text = "0";
txtAverage.Text = "0";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}