Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void TestPrintColors()
- {
- string[] colors = new string[4] {"red", "blue", "green", "yellow"};
- string str = "Lorem ipsum dolor sit amet";
- PrintColors(colors, str);
- }
- static void PrintColors(string[] colors, string str)
- {
- char log;
- ConsoleColor originalColor = Console.ForegroundColor;
- int colorIndex = 0;
- ConsoleColor currentColor = originalColor;
- for (int i = 0; i < str.Length; i++)
- {
- log = str[i];
- if (log == ' ')
- {
- Console.WriteLine(log);
- continue;
- }
- switch(colors[colorIndex])
- {
- case "red":
- currentColor = ConsoleColor.Red;
- break;
- case "blue":
- currentColor = ConsoleColor.Blue;
- break;
- case "green":
- currentColor = ConsoleColor.Green;
- break;
- case "yellow":
- currentColor = ConsoleColor.Yellow;
- break;
- default:
- currentColor = originalColor;
- break;
- }
- colorIndex++;
- if (colorIndex >= colors.Length)
- {
- colorIndex = 0;
- }
- Console.ForegroundColor = currentColor;
- Console.WriteLine(log);
- }
- Console.ForegroundColor = originalColor;
- }
- // before
- string[] colors = new string[4] {"red", "blue", "green", "yellow"};
- // after
- string[] colors = new string[] {"red", "blue", "green", "yellow"};
- // before
- colorIndex++;
- if (colorIndex >= colors.Length)
- {
- colorIndex = 0;
- }
- // after
- colorIndex = (colorIndex + 1) % colors.Length;
- // before
- for (int i = 0; i < str.Length; i++)
- {
- log = str[i];
- // after
- foreach (char log in str)
- {
- // before
- if (log == ' ')
- // after
- if (char.IsWhiteSpace(log))
- // before
- switch (colors[colorIndex])
- {
- case "red":
- currentColor = ConsoleColor.Red;
- break;
- case "blue":
- currentColor = ConsoleColor.Blue;
- break;
- case "green":
- currentColor = ConsoleColor.Green;
- break;
- case "yellow":
- currentColor = ConsoleColor.Yellow;
- break;
- default:
- currentColor = originalColor;
- break;
- }
- // after
- if (!Enum.TryParse(colors[colorIndex], true, out currentColor))
- {
- currentColor = originalColor;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement