ranee

сумма элементов массива

Apr 17th, 2021 (edited)
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             List<int> numbers = new List<int>() { 1, 2, 3};
  11.             string command;
  12.             bool exit = false;
  13.             while(exit == false)
  14.             {
  15.                 Console.Clear();
  16.                 Console.Write("Текуший массив: ");
  17.                 for (int i = 0; i < numbers.Count; i++)
  18.                 {
  19.                     Console.Write(numbers[i] + " ");
  20.                 }
  21.                 Console.WriteLine();
  22.                 Console.Write("Введите команду: ");
  23.                 command = Console.ReadLine();
  24.                 switch(command)
  25.                 {
  26.                     case "sum":
  27.                         int sumElement = 0;
  28.                         for (int i = 0; i < numbers.Count; i++)
  29.                         {
  30.                             sumElement += numbers[i];
  31.                         }
  32.                         Console.WriteLine($"Cумма элементов массива равна: {sumElement}.");
  33.                         Console.ReadKey();
  34.                         break;
  35.                     case "exit":
  36.                         exit = true;
  37.                         break;
  38.                     default:
  39.                         int number;
  40.                         if (int.TryParse(command, out number))
  41.                         {
  42.                             numbers.Add(number);
  43.                         }
  44.                         else
  45.                         {
  46.                             Console.WriteLine("Доступные команды для ввода: sum, exit, число от -2 147 483 648 до 2 147 483 647");
  47.                             Console.ReadKey();
  48.                         }
  49.                         break;
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment