数组越界?为什么?

时间:2014-09-03 12:14:51

标签: c# arrays indexoutofboundsexception

目前正在设计一个应用程序,该应用程序将在初始销售时计算销售额为4个月的销售额。此次出售预计将在4个月内每月增加5%。 ProduceSalesProjectionTable函数似乎给了我一个出界的错误,我不知道为什么似乎无法弄明白。

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

namespace itse
{
    class TempAgency
    {
        static string[] salesman1 = new string[4];
        static double[,] sales1 = new double[4, 5];
        static void DisplayInstructions()
        {
            Console.WriteLine("You will be asked to enter data for four salesmen.");
            Console.WriteLine("For their name, enter their first name followed");
            Console.WriteLine("by a space and then their las name.");
            Console.WriteLine("");
            Console.WriteLine("For testing purposes enter data for four <4> salesmen.");
            Console.WriteLine("");
            Console.WriteLine("");
        }

        static void GetSalesData(string[] salesman, double[,] sales) 
        {
        for (int x = 0; x < 4; x++) 
            {
                Console.WriteLine("Enter name: ");
                salesman[x] = Console.ReadLine();
                string [] split = salesman[x].Split(new Char [] {' '});
                Console.WriteLine(split[0]+"'s Sales goal: ");
                sales[x,0]=Convert.ToDouble(Console.ReadLine());
            }
        }
        static void ProduceSalesProjectionTable(double[,] sales)
        {
            for (int i = 0; i < 4; i++)
            {
                double salesPercentage = 0.05;
                for (int j = 0; j < 4; i++)
                {

                    sales[i, j + 1] = sales[i, 0] + (sales[i, 0] * salesPercentage);          //here lies the problem 
                    salesPercentage += 0.05;
                }
            }



        }
        static void DisplaySalesProjections()
        { 

        }

        public static void Main(String[] args)
        {

            DisplayInstructions();
            GetSalesData(salesman1, sales1);
            //Console.WriteLine("salesman: " + salesman[0]);
            //Console.WriteLine("sales: " + sales[0, 0]);
            ProduceSalesProjectionTable(sales1);
            //for (int i = 0; i < 4; i++)
            //{
            //    //double salesPercentage = 0.05;
            //    for (int j = 0; j < 4; i++)
            //    {
            //        Console.WriteLine("salesman:  " + salesman[i] + "\tsales: " + sales[i, j]);
            //    }
            //}
            //Console.WriteLine("sales:" + sales[0, 1]);
            //Console.WriteLine("sales:" + sales[1, 1]);
            //Console.WriteLine("sales:" + sales[2, 1]);
            //Console.WriteLine("sales:" + sales[3, 1]);
            for (int i = 0; i < 4; i++)
            {
                //double salesPercentage = 0.05;
                for (int j = 0; j < 4; i++)
                {
                    Console.WriteLine("salesman:  " + salesman1[i] + "\tsales: " +     sales1[i, 0]);
                }
            }
            Console.ReadLine();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

仔细查看

for (int j = 0; j < 4; i++)

您的索引器 j ,但您正在增加 i 而不是j。

最终sales[i, j + 1]评估为超出范围的sales[4, 1]

解决方案:将代码更改为

for (int j = 0; j < 4; j++)

(你已经在2个地方完成了这个)

答案 1 :(得分:0)

如果你像这样构建你的for循环,那么应该没有“出界错误”:

static void ProduceSalesProjectionTable(double[,] sales)
    {
        for (int i = 0; i < sales1.GetLength(0); i++)
        {             
            for (int j = 0; j < sales1.GetLength(1); i++)
            {
                ...Do Stuff...
            }
        }
    }

使用二维数组时 GetLength(0)会为您提供二维数组第一维的长度, GetLength(1)会为您提供第二维的长度。