2017-07-22 29 views
-1

我想在交錯數組來填充隨機數,但我在這行收到錯誤:在陣列填充隨機數字:ERROR:不能隱式int類型爲int []

arr[size] = randNum.Next(Min, Max); 

ERROR :不能隱式int類型爲int []

這裏是全碼:

Random randNum = new Random(); 
      int Min = 1; 
      int Max = 100; 

      int rows; 
      int size; 
      Console.WriteLine("Enter the number of rows of jagged array:"); 
      rows = int.Parse(Console.ReadLine()); 

      // Declare the array of two elements: 
      int[][] arr = new int[rows][]; 

      for (int i = 0; i <= rows; i++) 
      { 
       Console.WriteLine("Enter the size of" +rows +" :"); 
       size = int.Parse(Console.ReadLine()); 
       for (int j = 0; j <= size; j++) 
       { 
        arr[j] = new int[size]; 
        int n = 0; 
        while (n < size) 
        { 
         arr[size] = randNum.Next(Min, Max); 
        } 

       } 
      } 

可以在這方面的任何一個幫助嗎?

+1

_arr [i] [n] = randNum.Next(Min,Max); n ++ _ – Steve

回答

1

您需要遍歷數組的第一維(行),並詢問要保存在當前行中的數組的大小,之後,可以對該數組進行維度定位並填充該數組存儲在當前行中。你有一個不需要的內部for循環,因爲填充是在while循環中完成的

// Loop till rows - 1 
for (int i = 0; i < rows; i++) 
{ 
    Console.WriteLine("Enter the size for the array in the " + i + " row:"); 
    size = int.Parse(Console.ReadLine()); 
    arr[i] = new int[size]; 
    int n = 0; 
    while (n < size) 
    { 
     arr[i][n] = randNum.Next(Min, Max); 
     n++; 
    } 
} 
相關問題