Torgach

Создание бара v2

Jan 23rd, 2021 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Bar
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             bool isWork = true;
  14.  
  15.             Console.WriteLine("1 - Создать собственную Полоску;\n2 - создать начальную Полоску;\n3 - Выход");
  16.             while (isWork)
  17.             {
  18.                 Console.Write("\nВвод: ");
  19.                 switch (Console.ReadLine())
  20.                 {
  21.                     case "1":
  22.                         SetSettings(out int value, out int maxValue, out int barPositionX, out int barPositionY, out ConsoleColor color, out int userProcent, out bool isUserPercentageOfPaintedBar);
  23.                         DrawBar(value, maxValue, barPositionX, barPositionY, color, isUserPercentageOfPaintedBar, userProcent);
  24.                         break;
  25.                     case "2":
  26.                         DefaultSettings(out value, out maxValue, out barPositionX, out barPositionY, out color, out userProcent, out isUserPercentageOfPaintedBar);
  27.                         DrawBar(value, maxValue, barPositionX, barPositionY, color, isUserPercentageOfPaintedBar, userProcent);
  28.                         break;
  29.                     case "3":
  30.                         isWork = false;
  31.                         break;
  32.                     default:
  33.                         Console.WriteLine("Повторите ввод!");
  34.                         break;
  35.                 }
  36.             }
  37.         }
  38.         static void DefaultSettings(out int defaultValue, out int defaultMaxValue, out int defaultBarPositionX, out int defaultBarPositionY, out ConsoleColor defaultColor, out int userProcent, out bool isUserPercentageOfPaintedBar)
  39.         {
  40.             Random rand = new Random();
  41.             defaultValue = rand.Next(1, 15);
  42.             defaultMaxValue = defaultValue + rand.Next(1, 15);
  43.             defaultBarPositionX = rand.Next(0, 15);
  44.             defaultBarPositionY = rand.Next(0, 15);
  45.             defaultColor = ConsoleColor.Red;
  46.             userProcent = 33;
  47.             isUserPercentageOfPaintedBar = false;
  48.  
  49.             Console.Clear();
  50.         }
  51.         static void SetSettings(out int value, out int maxValue, out int barPositionX, out int barPositionY, out ConsoleColor color, out int userProcent, out bool isUserPercentageOfPaintedBar)
  52.         {
  53.             DefaultSettings(out value, out maxValue, out barPositionX, out barPositionY, out color, out userProcent, out isUserPercentageOfPaintedBar);
  54.  
  55.             bool breakOut = true;
  56.             int colorIndex;
  57.  
  58.             while (breakOut)
  59.             {
  60.                 Console.Write("Введите максимальное значение Полоски: ");
  61.                 maxValue = Convert.ToInt32(Console.ReadLine());
  62.  
  63.                 Console.WriteLine("1 - Ввести начальное значение Полоски;\n2 - Ввести процент от максимального значения Полоски;");
  64.                 Console.Write("Ввод: ");
  65.                 switch (Console.ReadLine())
  66.                 {
  67.                     case "1":
  68.                         Console.Write("Введите начальное значение Полоски: ");
  69.                         value = Convert.ToInt32(Console.ReadLine());
  70.                         break;
  71.                     case "2":
  72.                         Console.Write("Введите процент от максимального значения Полоски: ");
  73.                         userProcent = Convert.ToInt32(Console.ReadLine());
  74.                         isUserPercentageOfPaintedBar = true;
  75.                         break;
  76.  
  77.                 }
  78.  
  79.                 Console.Write("Укажите позицию Полоски - горизонталь: ");
  80.                 barPositionX = Convert.ToInt32(Console.ReadLine());
  81.  
  82.                 Console.Write("Укажите позицию Полоски - вертикаль: ");
  83.                 barPositionY = Convert.ToInt32(Console.ReadLine());
  84.  
  85.                 for (int i = 0; i < 3 && breakOut != false; ++i)
  86.                 {
  87.                     Console.Write("Укажите цвет Полоски от 1 до 16: ");
  88.                     colorIndex = (Convert.ToInt32(Console.ReadLine())) - 1;
  89.  
  90.                     if (colorIndex > 0 || colorIndex > 15)
  91.                     {
  92.                         color = (ConsoleColor)colorIndex;
  93.                         breakOut = false;
  94.                     }
  95.                     else
  96.                     {
  97.                         Console.WriteLine("Ошибка!");
  98.                     }
  99.                 }
  100.             }
  101.             Console.Clear();
  102.         }
  103.         static void DrawBar(int value, int maxValue, int barPositionX, int barPositionY, ConsoleColor color, bool isUserPercentageOfPaintedBar, int userProcent)
  104.         {
  105.             ConsoleColor defaultColor = Console.BackgroundColor;
  106.  
  107.             string bar = "";
  108.  
  109.             for (int i = 0; i < value; i++)
  110.             {
  111.                 bar += '#';
  112.             }
  113.  
  114.             Console.SetCursorPosition(barPositionX, barPositionY);
  115.  
  116.             Console.Write('[');
  117.             Console.BackgroundColor = color;
  118.             Console.Write(bar);
  119.             Console.BackgroundColor = defaultColor;
  120.  
  121.             bar = "";
  122.  
  123.             for (int i = value; i < maxValue; i++)
  124.             {
  125.                 bar += '_';
  126.             }
  127.  
  128.             Console.Write(bar + ']');
  129.  
  130.             if (isUserPercentageOfPaintedBar == false)
  131.             {
  132.                 double precent = Convert.ToDouble(value) / Convert.ToDouble(maxValue) * 100;
  133.                 Console.SetCursorPosition(barPositionX, barPositionY + 1);
  134.                 Console.WriteLine("Процент: " + Math.Truncate(precent));
  135.             }
  136.             else
  137.             {
  138.                 Console.SetCursorPosition(barPositionX, barPositionY + 1);
  139.                 Console.Write("Процент: " + userProcent);
  140.             }
  141.         }
  142.     }
  143. }
  144.  
Add Comment
Please, Sign In to add comment