Compare commits

..

2 commits

Author SHA1 Message Date
Geir Okkenhaug Jerstad 32933758fe Finished the lesson 2024-12-02 14:35:34 +01:00
Geir Okkenhaug Jerstad e1720b362f extract to method 2024-12-02 13:45:57 +01:00
2 changed files with 20 additions and 10 deletions

View file

@ -1,6 +1,7 @@

var numbers = new int[100];
var numberCount = 0;
using Innkapsling;
var stats = new Stats();
while (true)
{
Console.WriteLine("Skriv et tall (eller blankt for å avslutte: ");
@ -8,13 +9,12 @@ while (true)
if (string.IsNullOrWhiteSpace(numberStr)) break;
var number = Convert.ToInt32(numberStr);
numbers[numberCount] = number;
numberCount++;
var sum = numbers.Sum();
Console.Clear();
stats.AddNumber(number);
Console.Clear();
Console.WriteLine(
$"Antall tall: {numberCount} " +
$"Sum: {sum} " +
$"Snitt: {(float)sum / numberCount}"
$"Antall tall: {stats.NumberCount} " +
$"Sum: {stats.Sum} " +
$"Snitt: {stats.Mean}"
);
}

View file

@ -1,6 +1,16 @@
namespace Innkapsling;
public class Stats
internal class Stats
{
public int NumberCount { get; private set; }
public int Sum { get; private set; }
public float Mean => (float)Sum / NumberCount;
public void AddNumber(int number)
{
Sum += number;
NumberCount++;
}
}