Last active
May 16, 2023 16:52
-
-
Save Strelok78/4a12763eee75cf27af3d45a93b974416 to your computer and use it in GitHub Desktop.
The user enters the numbers, and the program remembers them. When user enters the sum command, the program outputs the sum of all the entered numbers. The program exits only if the user enters the exit command.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyCode | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string CommandSum = "sum"; | |
const string CommandExit = "exit"; | |
int[] ints = new int[0]; | |
int sumTextLine = 15; | |
int enterTextLine = 10; | |
string enter; | |
bool isRunning = true; | |
while (isRunning) | |
{ | |
Console.WriteLine($"Enter {CommandSum} to get sum of entered numbers.\n" + | |
$"Enter {CommandExit} to exit the program."); | |
Console.WriteLine("\n\nAlready entered numbers: "); | |
for (int i = 0; i < ints.Length; i++) | |
{ | |
Console.Write(ints[i] + " "); | |
} | |
Console.SetCursorPosition(0, enterTextLine); | |
switch (enter = Console.ReadLine()) | |
{ | |
case CommandSum: | |
int sum = 0; | |
if (ints != null || ints.Length != 0) | |
{ | |
for (int i = 0; i < ints.Length; i++) | |
{ | |
sum += ints[i]; | |
} | |
Console.SetCursorPosition(0, sumTextLine); | |
Console.WriteLine("Your sum is " + sum); | |
break; | |
} | |
Console.WriteLine(sum); | |
break; | |
case CommandExit: | |
isRunning = false; | |
break; | |
default: | |
bool correct = false; | |
foreach (char c in enter) | |
{ | |
if (char.IsDigit(c)) | |
{ | |
correct = true; | |
} | |
else | |
{ | |
correct = false; | |
} | |
} | |
if (correct) | |
{ | |
int[] tempNumberList = new int[ints.Length + 1]; | |
for (int i = 0; i < ints.Length; i++) | |
{ | |
tempNumberList[i] = ints[i]; | |
} | |
tempNumberList[tempNumberList.Length - 1] = int.Parse(enter); | |
ints = tempNumberList; | |
break; | |
} | |
Console.WriteLine("\nYour enter is incorrect! Enter command or a digit!"); | |
break; | |
} | |
Console.WriteLine("\n\nPress any button to continue..."); | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment