0

在我的数组上,第 40 行,我的边界在数组之外,但我不确定如何格式化它,因为这是我第一个使用多维数组的程序。请帮我。谢谢!(第 40 行是array[i, 0] = randomArray.Next(0, 100);

namespace Exercise6
{
    class Program
    {
        static void Main(string[] args)
        {

            OtherClass aTable = new OtherClass(); //instantiate class

            Console.WriteLine("How many rows do you want your two-dimensional array to be?");
            aTable.SRows = Console.ReadLine(); //reads input for how many rows that the user would like
            aTable.IntRows = int.Parse(aTable.SRows); //convert rows to int

            Console.WriteLine("Thanks you! How many columns would you like your two-dimensional arry to be?");
            aTable.SColumns = Console.ReadLine(); //reads input for how many columns that the user would like
            aTable.IntColumns = int.Parse(aTable.SColumns); //convert columns to int

            //set two dimensional array based upon the size that the user has requested

            int[ , ] array = new int[aTable.IntColumns, aTable.IntRows];

            Random randomArray = new Random(); //call to random class to ask for random numbers

            for (int i = 0; i <= aTable.IntColumns; i++) // rows
            {
                array[i, 0] = randomArray.Next(0, 100); // for every value in each row, insert a random number
            }
            //both arrays here are overloaded. See this site to see if can get help. Site is below after last close loop
            for (int y = 0; y <= aTable.IntRows; y++) // columns
            {
                array[y, y] = randomArray.Next(0, 100);
            }

            Console.WriteLine(array);


        }
    }
}

namespace Exercise6
{
    class OtherClass
    {
        private string sRows;

        public string SRows { get; set; }

        private int intRows;

        public int IntRows { get; set; }

        private string sColumns;

        public string SColumns { get; set; }

        private int intColumns;

        public int IntColumns { get; set; }

    }
}
4

4 回答 4

2

将循环条件更改为i < aTable.IntColumns

您循环从 0 开始并达到aTable.IntColumns - 1

和代码变成

for (int i = 0; i < aTable.IntColumns; i++)
{
    array[i, 0] = randomArray.Next(0, 100); 
}
于 2013-10-16T17:09:27.763 回答
2

由于数组是从零开始的,因此您不能达到最大值:

// This line is incorrect
for (int i = 0; i <= aTable.IntColumns; i++)

那行应该是:

for (int i = 0; i < aTable.IntColumns; i++)

这将让它从0到,这是长度aTable.IntColumns-1数组的有效索引。aTable.IntColumns行也是如此。

于 2013-10-16T17:10:25.683 回答
1

C# 数组使用零相对偏移量作为它们的索引。这意味着对于长度为n的数组,其索引i是。100 元素数组的索引范围为 0-99。如果您尝试用随机值填充 m*n 数组。0 <= i <= n-1

如果您的任务是用随机值填充数组(看起来很可能),您需要执行以下操作:

for ( int i=0 ; i < aTable.IntRows ; i++ ) // rows
{
  for ( int j= 0 ; i < aTable.IntColumns ; j++ )
  {
    array[i,j] = randomArray.Next(0,100); // for every value in each row, insert a random number
  }
}

您可能还注意到您的注释与您的代码不匹配:您正在遍历行并检查列限制,反之亦然。

于 2013-10-16T17:20:26.653 回答
0

错误在for循环中:

for (int i = 0; i < aTable.IntColumns; i++) // rows
{
    array[i, 0] = randomArray.Next(0, 100); // for every value in each row, insert a random number
}

i < aTable.IntColumns应该是停止标准

于 2013-10-16T17:10:38.797 回答