C#IndexOutof RangeException

时间:2014-06-06 19:06:52

标签: c# arrays mono

我收到一个System.IndexOutOfRangeException:索引超出了数组的范围。当我尝试运行我的编译代码(我正在使用C#)时,在EvEOreCalculator.Program.Main()。对于来自用户的输入,这些值可以达到50亿个区域。所有包含大量数字的int变量和数组都已转换为long。剩下的唯一一个就是在循环的标准for循环中迭代数组。

我没有任何问题,直到我更正了compressedOreList数组中的数据,这个数组和orelist都是int,直到我开始得到超出范围的异常,它作为一个int运行良好,两个表中的数字都只是在我做出改变之前,所有改变的都是数字本身。

这基本上是一个概念验证控制台应用程序,我将改为Windows窗体应用程序,我正在努力确保它背后的数学是正确的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace EvEOreCalculator
{
    class Program
    {
        static void Main()
        {
            //array to store the input values from the user

            //minerals[5] = new int inputZ;
            //minerals[6] = new int inputMega;

            /*
            Due to the amounts of Nocxium, Zydrine and Megacyte used in even titans being negligible in terms of space 
            (a Titan worth of Nocxium is not even half a JF) and to reduce the amounts of complexity of the problem
            as well as only wanting to account for high sec ores as they are the most available in trade hubs and thus most likely to be used,
            Nocxium totals will be subtracted if Pyroxeres is needed and will be removed from the total that is 
            sent back to the user.
            */

            //In the following array, the data is from left to right: {Tritanium, Pyerite, Mexallon, Isogen, Nocxium, Zydrine, Megacyte, and then
            //the volume per batch in meters cubed};
            //if a maximum volume is needed to reference use 367000m3
            long[,] orelist = new long[5, 8]
            {
            //high sec ores, very common
            {415,   0,      0,      0,      0,      0,      0,      10}, //POS 0 Veldspar 41.5 Trit/m3  
            {346,   173,    0,      0,      0,      0,      0,      15}, //POS 1 Scordite  23.0666 T/m3
            {351,   25,     50,     0,      5,      0,      0,      30}, //POS 2 Pyroxeres  11.7 T/m3
            {107,   213,    107,    0,      0,      0,      0,      35}, //POS 3 Plagioclase   3.057 t/m3
            {85,    34,     0,      85,     0,      0,      0,      60}, //POS 4 Omber      1.4166 t/m3
        //  {134,   0,      267,    134,    0,      0,      0,      120} //POS 5 Kernite   1.116666 t/m3
            };
            long[,] compressedOreList = new long[5, 6]
            {
            {690500,    0,          0,          0,          0,      257},//POS 0 compressed Veldspar
            {345112,    172349,     0,          0,          0,      193},//POS 1 compressed Scordite            
            {174835,    12222,      24858,      0,          2279,   80},//POS 2 compressed Pyroxeres            
            {35354,     70708,      35354,      0,          0,      53},//POS 3 compressed Plagioclase          
            {21199,     8494,       0,          21199,      0,      19},//POS 4 compressed Omber            
        //  {11580,     0,          23191,      11580,      0,      720} //POS 5 compressed Kernite
            };
            /*
            //Jaspet is garbage so it is excluded
            //orelist[6] = new int[8] {72,  121,    144,    0,      72,     0,      3,      200}; //Jaspet    0.36 t/m3


            //null sec ores, less common on market
            orelist[11] = new int[8]{6905,  0,      0,      0,      0,      230,    115,    1600}};  //Arkonor  4.315625 t/m3
            orelist[12] = new int[8]{0,     8286,   0,      0,      0,      118,    235,    1600};  //Bistot 
            orelist[14] = new int[8]{39221, 4972,   0,      0,      0,      78,     0,      1600} //Spodumain   24.5131 t/m3
            orelist[13] = new int[8]{20992, 0,      0,      0,      183,    0,      367,    1600}; //Crokite   13.12 t/m3

            //low sec ores, 
            orelist[7] = new int[8] {180,   72,     17,     159,    118,    0,      8,      300}; //Hemorphite  0.6 t/m3
            orelist[8] = new int[8] {0,     81,     0,      196,    98,     0,      9,      300};  //Hedbergite
            orelist[9] = new int[8] {1278,  0,      1278,   242,    0,      0,      60,     500};  //Gneiss   2.556 t/m3
            orelist[10] = new int[8]{8804,  0,      0,      0,      173,    0,      87,     800};  //Dark_Ochre  11.005 t/m3
            */

            //asking the user to input the amount of each mineral they need to carry
            //mineralsT array is  from left to right: {Tritanium, Pyerite, Mexallon, Isogen, Nocxium}
            long[] mineralsT = new long[5];

            Console.WriteLine("How much Tritanium:");
            string input = Console.ReadLine();
            mineralsT[0] =  long.Parse(input);
            //mineralsT[0] = Convert.ToInt32(input);

            Console.WriteLine("How much Pyerite:");
            string input1 = Console.ReadLine();
            mineralsT[1] = long.Parse(input1);

            Console.WriteLine("How much Mexallon:");
            string input2 = Console.ReadLine();
            mineralsT[2] = long.Parse(input2);

            Console.WriteLine("How much Isogen:");
            string input3 = Console.ReadLine();
            mineralsT[3] = long.Parse(input3);

            Console.WriteLine("How much Nocxium:");
            string input4 = Console.ReadLine();
            mineralsT[4] = long.Parse(input4);

            //Console.WriteLine("How much Zydrine:");
            //inputI = Console.ReadLine();
            //Console.WriteLine("How much Megacyte:");
            //inputI = Console.ReadLine();


            long[] oresTcount = new long[10];

            while (mineralsT[0] > 0)
            {
                while (mineralsT[3] > 0) //Isogen
                {
                    /*while (mineralsT[2] >= compressedOreList[2])
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - compressedOreList[5, i];
                        }
                        oresTcount[11]++;
                    }*/

                    while(mineralsT[3] >= compressedOreList[4, 3])  //compressed Omber
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - compressedOreList[4, i];
                        }
                        oresTcount[10]++;
                    }

                /*    while(mineralsT[3] < compressedOreList[4, 3] && mineralsT[2] >= compressedOreList[5, 3]) //compressed Kernite
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - compressedOreList[5, i];
                        }
                        oresTcount[11]++;
                    } 

                    while (mineralsT[2] < compressedOreList[5, 3] && mineralsT[2] >= 267 )  //Kernite
                     {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - orelist[5, i];
                        }
                        oresTcount[11]++;
                    } */

                    while(mineralsT[2] < 267 && mineralsT[3] > 0)  //Omber
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - orelist[4, i];
                        }
                        oresTcount[4] = oresTcount[4]+100;
                    }
                   // Console.WriteLine("Derp");
                }

                while (mineralsT[2] > 0)  //Mexallon
                {
                    while(mineralsT[2] >= compressedOreList[2, 2] && mineralsT[4] >= compressedOreList[2,4]) //compressed Pyroxeres
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - compressedOreList[2, i];
                        }
                        oresTcount[8]++;                    
                    } 
                    while(mineralsT[2] >= compressedOreList[3, 2]) //compressed Plagioclase
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - compressedOreList[3, i];
                        }
                        oresTcount[9]++;                    
                    }

                    if (mineralsT[2] <= compressedOreList[2, 2] && mineralsT[2]  >= 107  )  //Plagioclase
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - orelist[3, i];
                        }
                        oresTcount[3] = oresTcount[3]+100;
                    }

                    else if (mineralsT[2] < 107) //Pyroxeres
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - orelist[2, i];
                        }
                        oresTcount[2] = oresTcount[2]+100;
                    }
                   // Console.WriteLine("Derp1");
                }

                while (mineralsT[1] > 0) //Pyerite
                {
                    while (mineralsT[1] >= compressedOreList[1,1])  //Compressed Scordite
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - compressedOreList[1, i];
                        }
                        oresTcount[7]++;
                    }

                    while (mineralsT[1] < compressedOreList[1,1] && mineralsT[1] > 0)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mineralsT[i] = mineralsT[i] - orelist[1, i];
                        }
                        oresTcount[1] = oresTcount[1]+100;
                    }

                }

                while (mineralsT[1] <= 0 && mineralsT[2] <= 0 && mineralsT[3] <= 0 && mineralsT[0] >= compressedOreList[0,0]) //compressed Veldspar
                {
                    mineralsT[0] = mineralsT[0] - compressedOreList[0, 0];
                    oresTcount[6]++;
                }
                if    (mineralsT[1] <= 0 && mineralsT[2] <= 0 && mineralsT[3] <= 0 && mineralsT[0] <= compressedOreList[0,0])//Veldspar
                {
                   mineralsT[0] = mineralsT[0] - orelist[0, 0];
                   oresTcount[0] = oresTcount[0]+100;
                }
            }
            Console.WriteLine(mineralsT[0] + "T" + mineralsT[1] + "P" + mineralsT[2] + "M" + mineralsT[3] + "I" + mineralsT[4] + "N");

            Console.WriteLine("Compressed Veldspar:" + oresTcount[6]);
            Console.WriteLine("Veldspar:" + oresTcount[0]);
            Console.WriteLine("");

            Console.WriteLine("Compressed Scordite:" + oresTcount[7]);
            Console.WriteLine("Scordite:" + oresTcount[1]);
            Console.WriteLine("");

            Console.WriteLine("Compressed Pyroxeres:" + oresTcount[8]);
            Console.WriteLine("Pyroxeres:" + oresTcount[2]);
            Console.WriteLine("");

            Console.WriteLine("Compressed Plagioclase:" + oresTcount[9]);
            Console.WriteLine("Plagioclase:" + oresTcount[3]);
            Console.WriteLine("");

            Console.WriteLine("Compressed Omber:" + oresTcount[10]);
            Console.WriteLine("Omber:" + oresTcount[4]);
            Console.WriteLine("");

           /* Console.WriteLine("Compressed Kernite:" + oresTcount[11]);
            Console.WriteLine("Kernite:" + oresTcount[5]);
            Console.WriteLine(""); */

            long TotalV = new long();

            TotalV = ((oresTcount[0] * orelist[0,7]) + 
            (oresTcount[1] * orelist[1,7]) +    
            (oresTcount[2] * orelist[2,7]) + 
            (oresTcount[3] * orelist[3,7]) + 
            (oresTcount[4] * orelist[4,7]) +

            (oresTcount[6] * compressedOreList[0,5]) + 
            (oresTcount[7] * compressedOreList[1,5]) + 
            (oresTcount[8] * compressedOreList[2,5]) + 
            (oresTcount[9] * compressedOreList[3,5]) +
            (oresTcount[10] * compressedOreList[4,5]));

            Console.WriteLine("Total Volume:" + TotalV);
            Console.WriteLine("");
        }
    }
}

3 个答案:

答案 0 :(得分:1)

乍一看,不确定这是否是唯一的问题,但你宣布

 long[] oresTcount = new long[10];

然后致电

 oresTcount[10]++;

oresTcount数组的最大可用索引是9,(从索引0到9的10个元素)因此,如果要使用索引10,则需要使用

声明数组
 long[] oresTcount = new long[11];

顺便说一句,索引5处的值不会打印出来,也不会在您的代码中使用。

答案 1 :(得分:1)

如果您实际读取异常中的堆栈跟踪,您会看到该失败的行

oresTCount[10]++;

该变量oresTCount定义为new long[10],意味着其索引具有域0-9(含)。 10出局了。

答案 2 :(得分:0)

我认为问题出在你使用oresTcount上。它被定义为:

long[] oresTcount = new long[10];

但你引用了oresTcount [10]:

oresTcount[10]++;

Console.WriteLine("Compressed Omber:" + oresTcount[10]);

您只能使用oresTcount的当前定义来引用索引0-9。

您应该能够查看异常的堆栈跟踪以验证导致异常的行。