Update Lesson 2 Task 2

This commit is contained in:
Stanislav Mykhailenko 2023-03-19 14:13:44 +02:00
parent 722437f3f7
commit a60bfaf1cd
GPG key ID: 1E95E66A9C9D6A36

View file

@ -16,37 +16,40 @@ List<Person> people = new List<Person>()
int minimum; int minimum;
int maximum; int maximum;
do while (true)
{ {
Console.Write("Enter minimum age: "); Console.Write($"Enter minimum age: ");
} while (!int.TryParse(Console.ReadLine(), out minimum) || minimum < 0 || minimum > 130); if (int.TryParse(Console.ReadLine(), out minimum) || minimum >= 0 || minimum <= 130)
break;
}
do while (true)
{ {
Console.Write("Enter maximum age: "); Console.Write($"Enter maximum age: ");
userInput = Console.ReadLine(); if (int.TryParse(Console.ReadLine(), out maximum) || maximum >= minimum || maximum <= 130)
} while (!int.TryParse(Console.ReadLine(), out maximum) || maximum < minimum || maximum > 130); break;
}
var selected = from person in people where person.Age >= minimum && person.Age <= maximum select person; var selected = from person in people where person.Age >= minimum && person.Age <= maximum select person;
foreach (Person person in selected) foreach (Person person in selected)
{ {
Console.WriteLine(person); Console.WriteLine(person);
} }
class Person class Person
{ {
public string Name { get;} public string Name { get;}
public int Age { get;} public int Age { get;}
public override string ToString() public override string ToString()
{ {
return "Name: " + Name + ", Age: " + Age; return "Name: " + Name + ", Age: " + Age;
} }
public Person(string name, int age) public Person(string name, int age)
{ {
Name = name; Name = name;
Age = age; Age = age;
} }
} }