我試圖做一個程序,它會找出哪一個數組的一半加在一起是更大的範圍,但不斷收到這個錯誤,我似乎無法找出原因系統索引出的C#
「嘗試從數組後部開始工作時,第31行的」平均Mark.exe「中出現」System.IndexOutOfRangeException「類型的未處理異常。
我對c#很新,想到我在做什麼應該工作? 非常感謝!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace most_frequent_int
{
class Program
{
static void Main(string[] args)
{
halfCheck(new int[] {1,1,2,3});
}
static void halfCheck(int[] checkArray)
{
int fHalf = 0;
int sHalf=0;
//even method
if (checkArray.Length % 2 == 0)
{
for (int i= 0;i<checkArray.Length/2;i++)//check first half even
{
fHalf = fHalf + checkArray[i];
}
for (int i=checkArray.Length;i>checkArray.Length/2;i--)
{
sHalf = sHalf + checkArray[i];
Console.WriteLine(sHalf);
}
if (fHalf > sHalf)
{
Console.WriteLine("The first half is bigger");
}
else
{
Console.WriteLine("The second half is bigger");
}
Console.ReadLine();
}
//odd method
else
{
Console.WriteLine("odd");
}
}
}
}
調試器告訴你什麼時候你通過代碼? –
@哈維拉什你可以評論錯誤發生的地方 –
http://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-and-how-do-i-fix-it調試它並修復錯誤 –