在第49行上获取索引超出范围错误消息“如果(myNumber == myPartyAges [i])”,我不知道为什么。使用Visual Studio基本设置在这里是代码:
List<int> myPartyAges = new List<int>() { 24, 44, 54, 39, 17, 24, 21 };
int myNumber = myPartyAges[5];
bool contains = false;
for (int i = myPartyAges[5] - 1; i >= 1; i--)
{
if (myNumber == myPartyAges[i])
{
contains = true;
break;
}
}
if (contains == true)
{
Console.WriteLine("Number found.");
}
else
{
Console.WriteLine("Number not found.");
}
答案 0 :(得分:2)
您是想做
for (int i = 4; i >= 0; i--)
代替
for (int i = myPartyAges[5] - 1; i >= 1; i--)
?