Advertisement
Guest User

Play Catch

a guest
Oct 25th, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. namespace PlayCatch
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class StartUp
  8.     {
  9.         public static void Main()
  10.         {
  11.             List<string> input = Console.ReadLine()
  12.                 .Trim()
  13.                 .Split(new string[] { " " }, StringSplitOptions
  14.                 .RemoveEmptyEntries)
  15.                 .ToList();
  16.  
  17.             int exceptionCount = 0;
  18.  
  19.             while (exceptionCount < 3)
  20.             {
  21.                 try
  22.                 {
  23.                     string[] commandArray = Console.ReadLine()
  24.                                 .Trim()
  25.                                 .Split(new string[] { " " }, StringSplitOptions
  26.                                 .RemoveEmptyEntries)
  27.                                 .ToArray();
  28.  
  29.                     switch (commandArray[0])
  30.                     {
  31.                         case "Replace":
  32.                             int index = int.Parse(commandArray[1]); ;
  33.                             string element = commandArray[2];
  34.  
  35.                             input.RemoveAt(index);
  36.                             input.Insert(index, element);
  37.                             break;
  38.  
  39.                         case "Print":
  40.                             int startIndex = int.Parse(commandArray[1]);
  41.                             int endIndex = int.Parse(commandArray[2]);
  42.  
  43.                             Console.WriteLine(string.Join(", ", input.GetRange(startIndex, endIndex - startIndex + 1)));
  44.                             break;
  45.  
  46.                         case "Show":
  47.                             int indexPrint = int.Parse(commandArray[1]);
  48.  
  49.                             Console.WriteLine(input[indexPrint]);
  50.                             break;
  51.                     }
  52.                 }
  53.                 catch (FormatException)
  54.                 {
  55.                     Console.WriteLine("The variable is not in the correct format!");
  56.                     exceptionCount++;
  57.                 }
  58.                 catch (ArgumentException)
  59.                 {
  60.                     Console.WriteLine("The index does not exist!");
  61.                     exceptionCount++;
  62.                 }
  63.                 catch (SystemException)
  64.                 {
  65.                     Console.WriteLine("The variable is not in the correct format!");
  66.                     exceptionCount++;
  67.                 }
  68.             }
  69.  
  70.             Console.WriteLine(string.Join(", ", input));
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement