数组越界?为什么?

本文关键字:为什么 越界 数组 | 更新日期: 2023-09-27 18:20:25

当前正在设计一个应用程序,该应用程序将为首次销售计算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();
        }
    }
}

数组越界?为什么?

仔细查看行

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

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

最终CCD_ 1评估为超出边界的CCD_ 2。

解决方案:将代码更改为

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

(您已经在两个地方完成了此操作)

如果你像这样构建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)