Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace HomeWork
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string commandSum = "sum";
- const string commandExit = "exit";
- int[] numbers = new int[0];
- bool isWorking = true;
- while(isWorking)
- {
- Console.WriteLine($"Вводите числа. Когда захотите подсчитать их сумму, введите {commandSum}. " +
- $"\nДля выхода введите {commandExit}.");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case commandSum:
- if (numbers.Length > 0)
- {
- int sum = 0;
- foreach (int number in numbers)
- {
- sum += number;
- }
- Console.WriteLine($"Сумма всех введённых чисел: {sum}.");
- }
- else
- {
- Console.WriteLine("Вы не ввели ни одного числа.");
- }
- break;
- case commandExit:
- isWorking = false;
- break;
- default:
- if(Int32.TryParse(userInput, out int newNumber))
- {
- int[] tempNumbers = new int[numbers.Length + 1];
- for (int i = 0; i < numbers.Length; i++)
- {
- tempNumbers[i] = numbers[i];
- }
- tempNumbers[tempNumbers.Length - 1] = newNumber;
- numbers = tempNumbers;
- }
- else
- {
- Console.WriteLine("Такой команды нет.");
- }
- break;
- }
- Console.WriteLine("Для продолжения нажмите любую клавишу.");
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment