Deleting A Specified Element In An Array Using Random Class

后端 未结 4 979
余生分开走
余生分开走 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:03

    Arrays can't be dynamically resized in .NET, so you can't really 'remove' an item from an array (you could set it to 0, but I don't think that's what you want).

    Try using a List instead:

    List list = new List() { 1, 2, 3 };
    Console.WriteLine("The Number That Left Us Is");
    Random rnd = new Random();
    int r = rnd.Next(list.Count);
    int Left = (list[r]);
    list.Remove(Left);
    Console.WriteLine(Left);
    

提交回复
热议问题