Advertisement
Stray_Wolf

ArraySum

Apr 9th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Array_
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("Вводите целые числа, программа их запомнит, " +
  14.                 "при наборе комманды sum, выдаст сумму этих чисел, " +
  15.                 "\nпри наборе exit, произойдет выход из программы");
  16.             int[] array = new int[0];
  17.             while (true)
  18.             {
  19.                 string userInput = Console.ReadLine();
  20.                 if (userInput == "sum")
  21.                 {
  22.                     int sum = 0;
  23.                     for (int i = 0; i < array.Length; i++)
  24.                         sum += array[i];
  25.                     Console.WriteLine("Сумма: " + sum);
  26.                 }
  27.                 else if (userInput == "exit")
  28.                 {
  29.                     break;
  30.                 }
  31.                 else
  32.                 {
  33.                     int[] tempArray = new int[array.Length + 1];
  34.                     int someNumer = Convert.ToInt32(userInput);
  35.                     tempArray[tempArray.Length - 1] = someNumer;
  36.                     for (int i = 0; i < array.Length; i++)
  37.                         tempArray[i] = array[i];
  38.                     array = tempArray;  
  39.                 }
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement