Deleting A Specified Element In An Array Using Random Class

后端 未结 4 986
余生分开走
余生分开走 2021-01-29 12:37
class Check
{
     public static void Main()
     {         
              int[] arr1 = new int[] { 1, 2, 3 };
              Console.WriteLine(\"The Number That Left Us          


        
4条回答
  •  终归单人心
    2021-01-29 13:17

    Try to following it removes the item from the list and creates a new array without the specific item.

    static void Main(string[] args)
    {
           int[] arr1 = new int[] { 1, 2, 3 };
          Console.WriteLine("The Number That Left Us Is");
          Random rnd = new Random();
          int r = rnd.Next(arr1.Length);
    
          // Create a new array except the item in the specific location
          arr1 = arr1.Except(new int[]{arr1[r]}).ToArray();
          int Left = (arr1[r]);
          Console.WriteLine(Left);
    }
    

提交回复
热议问题