Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Clight_34_DynamicArrayAkaList
- {
- class Program
- {
- static void Main(string[] args)
- {
- int sum;
- string userInput;
- bool isExit = false;
- bool isNumber = false;
- List<int> numbers = new List<int>();
- while (!isExit)
- {
- Console.Clear();
- PrintText(5, $"Последовательность: ");
- PrintSequence(6, numbers);
- PrintText(9, "sum - посчитать сумму чисел последовательности");
- PrintText(10, "clear - очистить последовательность");
- PrintText(11, "exit - выход");
- PrintText(0, $"Введите число: ");
- userInput = Console.ReadLine();
- isNumber = Int32.TryParse(userInput, out int number);
- if (isNumber)
- {
- numbers.Add(number);
- }
- else if (userInput == "sum")
- {
- sum = 0;
- foreach (var item in numbers)
- {
- sum += item;
- }
- PrintText(2, $"Сумма чисел: {sum}");
- Console.ReadKey();
- }
- else if (userInput == "clear")
- {
- numbers.Clear();
- }
- else if (userInput == "exit")
- {
- isExit = true;
- }
- else
- {
- PrintText(2, "Не корректный ввод.", ConsoleColor.DarkRed);
- Console.ReadKey();
- }
- }
- }
- static void PrintText(int numRow, string words, ConsoleColor color = ConsoleColor.White)
- {
- Console.SetCursorPosition(0, numRow);
- Console.ForegroundColor = color;
- Console.Write(words);
- }
- static void PrintSequence(int numRow, List<int> numbers, ConsoleColor color = ConsoleColor.White)
- {
- Console.SetCursorPosition(0, numRow);
- Console.ForegroundColor = color;
- if (numbers.Count > 0)
- {
- foreach (var number in numbers)
- {
- Console.Write($"{number} ");
- }
- }
- else
- {
- Console.Write("<пусто>");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement