Количество элементов в списке удовлетворяющих условию - C#
Формулировка задачи:
Необходимо подсчитать количество элементов каждого типа, по критериям (компания, модель), в списке airplaines принадлежащих классу Airplane.
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Program
- {
- static List<Airplane> airplanes = new List<Airplane>();
- static void Main(string[] args)
- {
- View ();
- }
- public static void View ()
- {
- Console.Clear();
- Console.WriteLine("1-Самолётов в аэропорту\n2-Создать новый самолёт\n3-Найти самолёт для перелёта\n4-Выход");
- string i = Console.ReadLine();
- switch (i)
- {
- case "1":
- Console.Clear();
- Console.WriteLine("Всего самолётов-"+ airplanes.Count);
- Console.WriteLine("Для возврата нажмите Enter");
- Console.ReadLine();
- View();
- break;
- case "2":
- Console.Clear();
- Factory();
- Console.WriteLine("Для возврата нажмите Enter");
- Console.ReadLine();
- View();
- break;
- case "3":
- Console.Clear();
- Console.WriteLine("Длявозврата нажмите Enter");
- Console.ReadLine();
- View();
- break;
- case "4":
- break;
- default:
- Console.Clear();
- Console.WriteLine("Извините такого пункта нет");
- Console.WriteLine("Для возврата нажмите Enter");
- Console.ReadLine();
- View();
- break;
- }
- }
- public static void Factory()
- {
- int n;
- Console.WriteLine("1-Боинг\n2-Белавиа\n3-Аэрофлот");
- int i = Convert.ToInt32(Console.ReadLine());
- if (i == 1)
- {
- Console.Clear();
- Console.WriteLine("1-737\n2-777\n3-747");
- n = Convert.ToInt32(Console.ReadLine());
- if (n == 1)
- {
- Console.Clear();
- Airplane A1 = new Airplane("Боинг", "737", 103, 2592);
- airplanes.Add(A1);
- }
- else if (n == 2)
- {
- Console.Clear();
- Airplane A1 = new Airplane("Боинг", "777", 705, 6020);
- airplanes.Add(A1);
- }
- else if (n == 3)
- {
- Console.Clear();
- Airplane A1 = new Airplane("Боинг", "747", 818, 9800);
- airplanes.Add(A1);
- }
- else
- {
- Console.WriteLine("Error: Такого пункта несуществует");
- Console.ReadLine();
- }
- }
- else if (i==2)
- {
- Console.Clear();
- Console.WriteLine("1-\n2-\n3-");
- n = Convert.ToInt32(Console.ReadLine());
- }
- else if (i ==3)
- {
- }
- else
- {
- Console.Clear();
- Console.WriteLine("Error: Такого пункта несуществует\n Для возврата нажмите Enter");
- Console.ReadLine();
- }
- }
- }
- }
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Airplane
- {
- public string companu ;
- public string model ;
- public int sits ;
- public float road;
- public Airplane(string companu, string model, int sits, float road)
- {
- this.companu = companu;
- this.model = model;
- this.sits = sits;
- this.road = road;
- }
- }
- }
Решение задачи: «Количество элементов в списке удовлетворяющих условию»
textual
Листинг программы
- count = cars.Count(s => s.StartsWith("A"));
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д