Garloon

4.7

Sep 4th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 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 CSLight
  8. {
  9.     class Menu
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] array = new int[5] { 0, 1, 2, 3, 4 };
  14.             bool shuffle = true;
  15.  
  16.             Console.Write("Исходный массив: ");
  17.             for(int i = 0; i < array.Length; i++)
  18.             {
  19.                 Console.Write(array[i] + " ");
  20.             }
  21.             Console.WriteLine("\n\nКоманда \"shuffle\" чтобы перемешать элементы массива");
  22.             Console.WriteLine("Команда \"exit\" чтобы выйти из программы");
  23.  
  24.             while (shuffle == true)
  25.             {
  26.                 Console.Write("\nВведите команду: ");
  27.                 switch (Console.ReadLine())
  28.                 {
  29.                     case "shuffle":
  30.                         Shuffle(ref array);
  31.                         Console.Write("\nНовый массив: ");
  32.                         for (int i = 0; i < array.Length; i++)
  33.                         {
  34.                             Console.Write(array[i] + " ");
  35.                         }
  36.                         break;
  37.                     case "exit":
  38.                         shuffle = false;
  39.                         break;
  40.                     default:
  41.                         Console.Write("\nНеизвестная команда");
  42.                         break;
  43.                 }
  44.             }
  45.         }
  46.  
  47.         static void Shuffle(ref int[] array)
  48.         {
  49.             Random rand = new Random();
  50.             for (int i = array.Length - 1; i >= 1; i--)
  51.             {
  52.                 int j = rand.Next(i + 1);
  53.                 int temp = array[j];
  54.                 array[j] = array[i];
  55.                 array[i] = temp;
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment