-2
我想要生成5組座標(只是兩個整數在一起)但是我得到錯誤「索引超出了數組錯誤的界限」。任何幫助,將不勝感激。C#數組:索引超出了數組錯誤的界限
private void BtnAGenerate_Click(object sender, EventArgs e)
{
int TotalCoordinates = 0;
int[,] GenaratedCoordinates = new int[4, 1];
while (TotalCoordinates <5)
{
Random rnd = new Random();
int RandomNumberX = rnd.Next(1, 5); // creates a number between 1 and 5
int RandomNumberY = rnd.Next(1, 10); // creates a number between 1 and 10
GenaratedCoordinates[TotalCoordinates, 0] = RandomNumberX;
GenaratedCoordinates[TotalCoordinates, 1] = RandomNumberY;
TotalCoordinates++;
}
}
對於第二個索引,只有一個項目在索引0處,但您正在訪問索引1. – clcto
第一個插槽(0-3索引)中只有4個項目,但您將訪問插槽4 – ps2goat
'int [,] GenaratedCoordinates = new int [5,2]'。 VB具有其他功能,其中數組長度從零開始,因此vb中的「int(4,1)」在第一個槽中實際上具有5的長度,而第二個中的長度爲2。 https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx – ps2goat