只要我進入while循環,程序就會拋出索引超出範圍異常,而不會獲取行和列值。 getint是獲取用戶輸入的同時確保中值的範圍的方法二維數組索引超出範圍異常
while(filled>0)
{
row = getint("row: ");
row--;
column = getint("column: ");
column--;
fields[row, column]=getint("value: ");
filled--;
}
編輯:
static int getint(string question)
{
string temp;
int tempint=0;
while (check != 0)
{
check = 0;
Console.Write("\nPlease enter the {0}",question);
temp = Console.ReadLine();
if(!int.TryParse(temp, out tempint))
{
check++;
}
if(question.Length<14 & (tempint<1 | tempint>9))
{
check++;
Console.WriteLine("Invalid Input");
}
}
return tempint;
}
你是什麼意思的「沒有獲得行和列的價值」?我們不知道這些方法是做什麼的,也不知道「字段」的大小。請提供[mcve]。 –
如果您在進入while循環時拋出異常,那麼問題可能出現在'getint()'中,但是您未包含該代碼。 – itsme86
需要更多代碼。例如,這是否意味着從底部到頂部? –