ranee

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

Jun 23rd, 2020 (edited)
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Dynamic;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Runtime.Serialization;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace CSLight
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             int[] numbers = new int[0];
  19.             string command;
  20.             int sumElement = 0;
  21.             while(true)
  22.             {
  23.                 for (int i = 0; i < numbers.Length; i++)
  24.                 {
  25.                     Console.Write(numbers[i] + " ");
  26.                 }
  27.                 Console.WriteLine();
  28.                 Console.Write("Введите команду: ");
  29.                 command = Console.ReadLine();
  30.                 if(command.ToLower() == "exit")
  31.                 {
  32.                     break;  
  33.                 }
  34.                 else if(command.ToLower() == "sum")
  35.                 {
  36.                     for(int i = 0; i < numbers.Length; i++)
  37.                     {
  38.                         sumElement += numbers[i];
  39.                     }
  40.                     Console.WriteLine($"Cумма элементов массива равна: {sumElement}.");
  41.                     sumElement = 0;
  42.                 }
  43.                 else if(Convert.ToInt32(command) > int.MinValue && Convert.ToInt32(command) < int.MaxValue)
  44.                 {
  45.                     int[] tempNumbers = new int[numbers.Length + 1];
  46.                     for(int i = 0; i < numbers.Length; i++)
  47.                     {
  48.                         tempNumbers[i] = numbers[i];
  49.                     }
  50.                     tempNumbers[tempNumbers.Length - 1] = Convert.ToInt32(command);
  51.                     numbers = tempNumbers;
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment