Advertisement
TwinFrame

DynamicArrayAkaList

Dec 10th, 2020 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Clight_34_DynamicArrayAkaList
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int sum;
  11. string userInput;
  12. bool isExit = false;
  13. bool isNumber = false;
  14. List<int> numbers = new List<int>();
  15.  
  16. while (!isExit)
  17. {
  18. Console.Clear();
  19. PrintText(5, $"Последовательность: ");
  20. PrintSequence(6, numbers);
  21. PrintText(9, "sum - посчитать сумму чисел последовательности");
  22. PrintText(10, "clear - очистить последовательность");
  23. PrintText(11, "exit - выход");
  24. PrintText(0, $"Введите число: ");
  25. userInput = Console.ReadLine();
  26. isNumber = Int32.TryParse(userInput, out int number);
  27.  
  28. if (isNumber)
  29. {
  30. numbers.Add(number);
  31. }
  32. else if (userInput == "sum")
  33. {
  34. sum = 0;
  35.  
  36. foreach (var item in numbers)
  37. {
  38. sum += item;
  39. }
  40.  
  41. PrintText(2, $"Сумма чисел: {sum}");
  42. Console.ReadKey();
  43. }
  44. else if (userInput == "clear")
  45. {
  46. numbers.Clear();
  47. }
  48. else if (userInput == "exit")
  49. {
  50. isExit = true;
  51. }
  52. else
  53. {
  54. PrintText(2, "Не корректный ввод.", ConsoleColor.DarkRed);
  55. Console.ReadKey();
  56. }
  57. }
  58. }
  59. static void PrintText(int numRow, string words, ConsoleColor color = ConsoleColor.White)
  60. {
  61. Console.SetCursorPosition(0, numRow);
  62. Console.ForegroundColor = color;
  63. Console.Write(words);
  64. }
  65. static void PrintSequence(int numRow, List<int> numbers, ConsoleColor color = ConsoleColor.White)
  66. {
  67. Console.SetCursorPosition(0, numRow);
  68. Console.ForegroundColor = color;
  69.  
  70. if (numbers.Count > 0)
  71. {
  72. foreach (var number in numbers)
  73. {
  74. Console.Write($"{number} ");
  75. }
  76. }
  77. else
  78. {
  79. Console.Write("<пусто>");
  80. }
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement