kwest87

Untitled

Jan 11th, 2024
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp23
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             const string CommandSum = "sum";
  10.             const string CommandExit = "exit";
  11.  
  12.             string userInput;
  13.             bool isWork = true;
  14.             int[] numbers = new int[0];
  15.  
  16.             while (isWork)
  17.             {
  18.                 Console.WriteLine($"Введите цифру ,либо {CommandSum} , либо {CommandExit} : ");
  19.                 userInput = Console.ReadLine();
  20.  
  21.                 if (userInput == CommandExit)
  22.                 {
  23.                     isWork = false;
  24.                 }
  25.                 else if (userInput == CommandSum)
  26.                 {
  27.                     int sum = 0;
  28.  
  29.                     for (int i = 0; i < numbers.Length; i++)
  30.                     {
  31.                         sum += numbers[i];
  32.                     }
  33.  
  34.                     Console.WriteLine(sum);
  35.                 }
  36.                 else
  37.                 {
  38.                     int[] temporaryNumbers = new int[numbers.Length + 1];
  39.  
  40.                     for (int i = 0; i < numbers.Length; i++)
  41.                     {
  42.                         temporaryNumbers[i] = numbers[i];
  43.                     }
  44.  
  45.                     temporaryNumbers[temporaryNumbers.Length - 1] = Convert.ToInt32(userInput);
  46.                     numbers = temporaryNumbers;
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment