Vla_DOS

ClassroomDesign

Mar 14th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ClassroomDesign
  5. {
  6.     public class Student
  7.     {
  8.         public string FirstName { get; set; }
  9.         public string LastName { get; set; }
  10.         public string ClassName { get; set; }
  11.     }
  12.     class Program
  13.     {
  14.         public static void InputList(List<Student> students, int n)
  15.         {
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 var student = new Student();
  19.                 Console.Write("Введiть iм'я: ");
  20.                 student.FirstName = Console.ReadLine();
  21.  
  22.                 Console.Write("Введiть прiзвище: ");
  23.                 student.LastName = Console.ReadLine();
  24.  
  25.                 Console.Write("Введiть назву класу: ");
  26.                 student.ClassName = Console.ReadLine();
  27.  
  28.                 Console.WriteLine();
  29.                 students.Add(student);
  30.             }
  31.         }
  32.         public static void OutputList(List<Student> students)
  33.         {
  34.             foreach (var i in students)
  35.                 Console.WriteLine($" Iм'я: {i.FirstName}. Прiзвище: {i.LastName}. Назва класу: {i.ClassName}.\n");
  36.         }
  37.         public static bool WhetherTheSameSurnames(List<Student> students, int n)
  38.         {
  39.             uint count = 0;
  40.             for (int i = 0; i < n; i++)
  41.             {
  42.                 if (i + 1 == n) break;
  43.                 if (students[i].LastName == students[i + 1].LastName) count++;  
  44.             }            
  45.             return count > 0;
  46.         }
  47.         static void Main(string[] args)
  48.         {
  49.             var stutlist = new List<Student>();
  50.             Console.Write("Вкажiть кiлькiсть учнiв: ");
  51.             try
  52.             {
  53.                 int n = int.Parse(Console.ReadLine());
  54.                 InputList(stutlist, n);
  55.                 OutputList(stutlist);
  56.  
  57.                 if (WhetherTheSameSurnames(stutlist, n))
  58.                     Console.WriteLine("\n В школi Є учнi з однаковим прiзвищем!");
  59.                 else
  60.                     Console.WriteLine("\n В школi НЕМАЄ учнiв з однаковим прiзвищем!");
  61.             }
  62.             catch(Exception ex)
  63.             {
  64.                 Console.WriteLine(ex.Message);
  65.             }
  66.             Console.ReadKey();
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment