嗨,我正在進行等級計算。我的問題在於如果字符串數組的長度比int數組更長,它會跳過最後2個等級。 例如:在C中傳遞數組#
int[] unit = new int[] {1,-3,3,4};
string[] letter_grade = new string[] {"A", "B","B","W","D","F"};
但如果int數組比字符串數組更長的長度它不工作的投擲的錯誤索引是該數組的範圍之外。
int[] unit = new int[] {1,-3,3,4,5,6,7};
string[] letter_grade = new string[] {"A", "B","B"};
所以我的問題我如何使它適用於兩者?
int length = unit.Length;
int no_units = length;
double totalGrade_Points = 0.0;
int totalno_units = 0;
totalGPA = 0;
for (int i = 0; i < unit.Length; i++)
{
entrygot = findGpaListentry(letter_grade[i]); //Index was outside the bounds of the array.
if (entrygot != null)
{
//some code calculation
}
}
所以你試圖訪問一個超出界限的索引。問題是什麼? (請注意'i'在範圍內[[0,unit.length]'..) – 2012-08-07 22:54:58