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

View file

@ -1,6 +1,16 @@
namespace Innkapsling; 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++;
}
} }