Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. static void TestPrintColors()
  2. {
  3. string[] colors = new string[4] {"red", "blue", "green", "yellow"};
  4. string str = "Lorem ipsum dolor sit amet";
  5. PrintColors(colors, str);
  6. }
  7.  
  8. static void PrintColors(string[] colors, string str)
  9. {
  10. char log;
  11. ConsoleColor originalColor = Console.ForegroundColor;
  12. int colorIndex = 0;
  13. ConsoleColor currentColor = originalColor;
  14. for (int i = 0; i < str.Length; i++)
  15. {
  16. log = str[i];
  17.  
  18. if (log == ' ')
  19. {
  20. Console.WriteLine(log);
  21. continue;
  22. }
  23.  
  24. switch(colors[colorIndex])
  25. {
  26. case "red":
  27. currentColor = ConsoleColor.Red;
  28. break;
  29.  
  30. case "blue":
  31. currentColor = ConsoleColor.Blue;
  32. break;
  33.  
  34. case "green":
  35. currentColor = ConsoleColor.Green;
  36. break;
  37.  
  38. case "yellow":
  39. currentColor = ConsoleColor.Yellow;
  40. break;
  41.  
  42. default:
  43. currentColor = originalColor;
  44. break;
  45. }
  46.  
  47. colorIndex++;
  48.  
  49. if (colorIndex >= colors.Length)
  50. {
  51. colorIndex = 0;
  52. }
  53.  
  54. Console.ForegroundColor = currentColor;
  55.  
  56. Console.WriteLine(log);
  57. }
  58.  
  59. Console.ForegroundColor = originalColor;
  60. }
  61.  
  62. // before
  63. string[] colors = new string[4] {"red", "blue", "green", "yellow"};
  64.  
  65.  
  66. // after
  67. string[] colors = new string[] {"red", "blue", "green", "yellow"};
  68.  
  69. // before
  70. colorIndex++;
  71.  
  72. if (colorIndex >= colors.Length)
  73. {
  74. colorIndex = 0;
  75. }
  76.  
  77.  
  78. // after
  79. colorIndex = (colorIndex + 1) % colors.Length;
  80.  
  81. // before
  82. for (int i = 0; i < str.Length; i++)
  83. {
  84. log = str[i];
  85.  
  86.  
  87. // after
  88. foreach (char log in str)
  89. {
  90.  
  91. // before
  92. if (log == ' ')
  93.  
  94. // after
  95. if (char.IsWhiteSpace(log))
  96.  
  97. // before
  98. switch (colors[colorIndex])
  99. {
  100. case "red":
  101. currentColor = ConsoleColor.Red;
  102. break;
  103.  
  104. case "blue":
  105. currentColor = ConsoleColor.Blue;
  106. break;
  107.  
  108. case "green":
  109. currentColor = ConsoleColor.Green;
  110. break;
  111.  
  112. case "yellow":
  113. currentColor = ConsoleColor.Yellow;
  114. break;
  115.  
  116. default:
  117. currentColor = originalColor;
  118. break;
  119. }
  120.  
  121. // after
  122. if (!Enum.TryParse(colors[colorIndex], true, out currentColor))
  123. {
  124. currentColor = originalColor;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement