Finished the lesson

This commit is contained in:
Geir Okkenhaug Jerstad 2024-12-02 14:35:34 +01:00
parent e1720b362f
commit 32933758fe
2 changed files with 13 additions and 21 deletions

View file

@ -10,6 +10,11 @@ while (true)
var number = Convert.ToInt32(numberStr); var number = Convert.ToInt32(numberStr);
stats.AddNumber(number); stats.AddNumber(number);
stats.Show(); Console.Clear();
Console.WriteLine(
$"Antall tall: {stats.NumberCount} " +
$"Sum: {stats.Sum} " +
$"Snitt: {stats.Mean}"
);
} }

View file

@ -1,29 +1,16 @@
namespace Innkapsling; namespace Innkapsling;
public class Stats internal class Stats
{ {
private int[] _numbers; public int NumberCount { get; private set; }
private int _numberCount; public int Sum { get; private set; }
public Stats() public float Mean => (float)Sum / NumberCount;
{
_numbers = new int[10];
}
public void AddNumber(int number) public void AddNumber(int number)
{ {
_numbers[_numberCount] = number; Sum += number;
_numberCount++; NumberCount++;
} }
public void Show()
{
var sum = _numbers.Sum();
Console.Clear();
Console.WriteLine(
$"Antall tall: {_numberCount} " +
$"Sum: {sum} " +
$"Snitt: {(float)sum / _numberCount}"
);
}
} }