Skip to content

Instantly share code, notes, and snippets.

@Lanse505
Created November 15, 2021 15:42
Show Gist options
  • Save Lanse505/a71d1b6249bc269602f425f4e5b32fdd to your computer and use it in GitHub Desktop.
Save Lanse505/a71d1b6249bc269602f425f4e5b32fdd to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Text;
namespace FirstAssignmentSubmission{
internal class Program{
private struct Person{
public Person(int index, string name, int age){
Index = index;
Name = name;
Age = age;
}
public int Index{ get; }
public string Name{ get; }
public int Age{ get; }
}
public static void Main(string[] args){
int totalAge = 0;
List<Person> people = new List<Person>();
KeyValuePair<int, int> youngest = new KeyValuePair<int, int>(-1, 500);
KeyValuePair<int, int> oldest = new KeyValuePair<int, int>(-1, -1);
for (int i = 0; i < 4; i++){
var name = $"Skriv namnet på person {i + 1}";
var age = $"Skriv åldern på person {i + 1}";
Console.WriteLine(name);
var inputName = Console.ReadLine();
Console.WriteLine(age);
var inputAge = Console.ReadLine();
int parsedAge;
try{
parsedAge = int.Parse(inputAge);
}
catch (Exception e){
Console.WriteLine(e);
throw;
}
if (parsedAge < youngest.Value){
youngest = new KeyValuePair<int, int>(i, parsedAge);
}
if (parsedAge > oldest.Value){
oldest = new KeyValuePair<int, int>(i, parsedAge);
}
people.Add(new Person(i, inputName, parsedAge));
totalAge += parsedAge;
}
foreach (var person in people){
StringBuilder builder = new StringBuilder($"{person.Name} är {person.Age} gammal.");
var isOldest = oldest.Key == person.Index;
var isYoungest = youngest.Key == person.Index;
if (isOldest){
builder.Append($" Vilket är {person.Age * 365} ungefär dagar gammal!");
builder.Append($" {person.Name} är Äldst!");
}
else if (isYoungest){
builder.Append($" Vilket är {person.Age * 365} ungefär dagar gammal!");
builder.Append($" {person.Name} är Yngst!");
}
else{
builder.Append($" Vilket är {person.Age * 365} ungefär dagar gammal!");
}
Console.WriteLine(builder.ToString());
}
var ageTotal = $"Sammanlagd ålder är: {totalAge}";
var ageAverage = $"Medelåldern är {totalAge / 4}";
Console.WriteLine(ageTotal);
Console.WriteLine(ageAverage);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment