Advertisement
Vlad_Savitskiy

Bar

Apr 14th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System;
  2.  
  3. namespace CSLightFirst
  4. {
  5.     class Program
  6.     {
  7.         static void Main()
  8.         {
  9.             DrawBar(20, 100, 0, ConsoleColor.DarkCyan);
  10.             DrawBar(10, 50, 1, ConsoleColor.Red, '#');
  11.             DrawBar(8, 40, 2, ConsoleColor.Blue);
  12.         }
  13.  
  14.         private static void DrawBar(int value, int percent, int position, ConsoleColor color, char symbol = ' ')
  15.         {
  16.             ConsoleColor defaultColor = Console.BackgroundColor;
  17.             Console.SetCursorPosition(0, position);
  18.  
  19.             int segmentCount = Convert.ToInt32(value * (percent / 100f));
  20.             Console.Write('[');
  21.             Console.BackgroundColor = color;
  22.             for (int i = 0; i < value; i++)
  23.             {
  24.                 if (segmentCount > 0)
  25.                 {
  26.                     Console.Write(symbol);
  27.                     segmentCount--;
  28.                 }
  29.                 else
  30.                 {
  31.                     Console.BackgroundColor = defaultColor;
  32.                     Console.Write(' ');
  33.                 }
  34.             }
  35.  
  36.             Console.BackgroundColor = defaultColor;
  37.             Console.Write(']');
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement