Warm tip: This article is reproduced from serverfault.com, please click
c#

c#-这段代码给了我一个错误,我不知道为什么?

(c# - This piece of code gives me an error, and I can't figure out why?)

发布于 2021-10-20 20:40:23

我正在尝试使用以下代码在该结构数组中找到最大的数字:

max=0;
for (i = 1; i <= team.Length; i++)
{
    if (team[i].Point > team[max].Point) 
    { 
        max = i; 
    }

它给了我一个错误System.OutOfRangeException请帮忙。

Questioner
Csetox
Viewed
0
JoelFan 2021-10-21 04:45:51

C# 中的数组是从 0 开始的,而不是从 1 开始的。改变你的for循环:

for (i = 1; i < team.Length; i++)

注意:根据@juharr 的反馈编辑