Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ConsoleApp1;
- using System.Text;
- public class Task23
- {
- public void Run()
- {
- Console.InputEncoding = Encoding.Unicode;
- Console.OutputEncoding = Encoding.Unicode;
- string sumCommand = "sum";
- string exitCommand = "exit";
- int[] numbers = {};
- int emptyArrayLength = 0;
- bool canExit = false;
- while ( canExit == false )
- {
- Console.WriteLine( "\nEnter commands 'sum' or 'exit' or numbers:" );
- string command = Console.ReadLine().Trim();
- if ( string.Equals( command, exitCommand ) )
- {
- canExit = true;
- }
- else if ( string.Equals( command, sumCommand ) )
- {
- if ( numbers.Length == emptyArrayLength )
- {
- Console.WriteLine("Array is empty, you can not calculate sum");
- }
- else
- {
- var arraySum = 0;
- foreach ( int number in numbers )
- arraySum += number;
- Console.WriteLine( $"Sum of array numbers is {arraySum}" );
- }
- }
- else
- {
- int number = int.Parse( command );
- int newNumbersArrayLength = numbers.Length + 1;
- int[] newNumbers = new int[newNumbersArrayLength];
- for ( int i = 0; i < numbers.Length; i++ )
- newNumbers[i] = numbers[i];
- newNumbers[newNumbers.Length - 1] = number;
- numbers = newNumbers;
- Console.WriteLine( $"Number {number} added" );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement