namespace projGetMaxScore
{
class Program
{
static void Main(string[] args)
{
int[] sorce = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
string[] name = new string[] { "89", "90", "98", "56", "60", "91", "93", "85" };
int Topsorce = 0;
string Name = "";
// foreach (int x in sorce)
//{
// if (sorce[x] >= Topsorce)
// Topsorce = sorce[x];
// Name = name[x];
// }
for(int i=0;i<sorce.GetLongLength(0);i++)
{
if (sorce[i] >= Topsorce)
Topsorce = sorce[i];
Name = name[i]; }
Console.Write("分数最高的是{0},分数是{1}", Name, Topsorce);
}
}
}
x指的是sorce 数组的1~8吧 后面的sorce[8]的时候角标越界了
foreach中的x是迭代变量名,不能对其进行操作,所以for就不会。例如foreach中写成x++就错了