I am learning C# like 1 or 2 months and I wanted to know if there is any possibility to shorten this code.
It is simple. It only changes color of the console text with while
loop if you enter wrong input. When you enter it correctly it changes the color + asks you if you want to continue.
It looks weird for me when I have for each color duplicity of the code written before (in if and switch statements) But I have no idea if there is any way to make it cleaner and shorter, but I guess there is. Also I would like to know where is worth to use arrays and where normal strings and integers + if there is any reasonable difference. Also I find it very hard for orientation when I have a lot of strings or ints inside array, but easy to maintain. Thanks for answers
static void Main(string[] args)
{
string[] Arrays = new string[3]{
"Hello!", "Enter a color\n1 = blue\n2 = red\n3 = Magenta", "Render numbers"
Console.WriteLine(Arrays[0]);
bool Continue = true;
while(Continue) {
Console.WriteLine(Arrays[1]);
string[] color = new string[4]{
"Blue", "Red", "Magenta", "Render numbers : "
};
char EnterColor = Console.ReadKey().KeyChar;
Console.WriteLine();
switch (EnterColor) {
case '1':
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Do you want to continue? y/n");
string NextTask = Console.ReadLine();
if (NextTask == "y") {
// set loop to repeat
Continue = true;
}
else {
Continue = false;
}
break;
case '2':
Console.ForegroundColor = ConsoleColor.Red;
Continue = false;
Console.WriteLine("Do you want to continue? y/n");
string NextTaskRed = Console.ReadLine();
if (NextTaskRed == "y") {
Continue = true;
}
else {
Continue = false;
}
break;
case '3':
Console.ForegroundColor = ConsoleColor.Magenta;
Continue = false;
Console.WriteLine("Do you want to continue? y/n");
string NextTasMagenta = Console.ReadLine();
if (NextTasMagenta == "y") {
// set loop to repeat
Continue = true;
}
else {
Continue = false;
}
break;
// there will be render nums option in next case using for loop
}
}
}