Anonim_999

2.6

Dec 22nd, 2022
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp27
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             List<int> numbers = new List<int>();
  11.             string userInput;
  12.             bool isWorking = true;
  13.             int number;
  14.             int positive;
  15.  
  16.             while (isWorking)
  17.             {
  18.                 Console.Write("Введите число: ");
  19.                 userInput = Console.ReadLine();
  20.  
  21.                 if (userInput == "0")
  22.                 {
  23.                     isWorking = false;
  24.  
  25.                     break;
  26.                 }
  27.                 else
  28.                 {
  29.                     if (int.TryParse(userInput,out number))
  30.                         numbers.Add(number);
  31.                     else
  32.                         Console.WriteLine("Вводите только числа!");
  33.                 }
  34.             }
  35.             ShowCollection(numbers);
  36.             PosNegSeq(numbers, out positive);
  37.             Console.WriteLine($"Кол-во положительных: {positive}\n" +
  38.                 $"Кол-во отрицательных: {numbers.Count - positive}");
  39.         }
  40.  
  41.         private static void ShowCollection<T>(List<T> collection)
  42.         {
  43.             foreach (T item in collection)
  44.                 Console.Write($"{item} ");
  45.         }
  46.  
  47.         private static void PosNegSeq(List<int> numbers, out int positive)
  48.         {
  49.             positive = 0;
  50.  
  51.             foreach (int number in numbers)
  52.                 if (number > 0)
  53.                     positive += 1;
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment