NikaBang

ДЗ: Консольное меню

Oct 6th, 2025
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | Gaming | 0 0
  1. using System;
  2. using System.Data;
  3.  
  4. internal class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         const string CommandShowText = "1";
  9.         const string CommandShowTime = "2";
  10.         const string CommandShowRandomNumber = "3";
  11.         const string CommandClearConsole = "4";
  12.         const string CommandExit = "5";
  13.  
  14.         bool isWork = true;
  15.  
  16.         while (isWork)
  17.         {
  18.             Console.WriteLine($"{CommandShowText} - показать текст." +
  19.                 $"\n{CommandShowTime} - показать время." +
  20.                 $"\n{CommandShowRandomNumber} - показать случайное число." +
  21.                 $"\n{CommandClearConsole} - отчистить консоль." +
  22.                 $"\n{CommandExit} - завершить программу.");
  23.  
  24.             string userInput = Console.ReadLine();
  25.  
  26.             switch (userInput)
  27.             {
  28.                 case CommandShowText:
  29.                     Console.WriteLine("Hello World!");
  30.                     break;
  31.  
  32.                 case CommandShowTime:
  33.                     Console.WriteLine(DateTime.Now);
  34.                     break;
  35.  
  36.                 case CommandShowRandomNumber:
  37.                     Random random = new Random();
  38.                     Console.WriteLine(random.Next());
  39.                     break;
  40.  
  41.                 case CommandClearConsole:
  42.                     Console.Clear();
  43.                     break;
  44.  
  45.                 case CommandExit:
  46.                     isWork = false;
  47.                     Console.WriteLine("Программа завершена.");
  48.                     break;
  49.  
  50.                 default:
  51.                     Console.WriteLine("Ошибка ввода!");
  52.                     break;
  53.             }
  54.         }
  55.  
  56.         Console.ReadKey();
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment