【问题标题】:Searching for value within 2d Array then returning true or false在二维数组中搜索值,然后返回 true 或 false
【发布时间】:2015-06-29 09:57:51
【问题描述】:

我有一个用随机数填充的数组。用户输入一个数字,然后我的程序应该在数组中找到它并显示找到它的行和列索引。进行搜索的是SearchArray() 方法。我的程序目前会要求输入一个数字,然后如果我输入 15 到 96 之间的任何内容,它会崩溃请帮助!

SearchArray() 方法将返回一个布尔值以指示是否找到了搜索的数字。它的参数应该有:一个整数,它是要搜索的数字,一个要搜索的二维数组,一个表示行索引的输出引用整数参数和一个表示列索引的输出引用整数参数方法将使用第一个参数和二维数组,并在数组中搜索 GetNumber() 方法选择的数字。我必须将行和列参数初始化为-1。当程序遍历数组时,如果找到数字,将行和列参数分配给找到它的行和列索引号,并立即停止搜索数组。该方法应该返回一个布尔值来指示是否找到了数字。

当它崩溃时它告诉我:索引超出了数组的边界

这是我的代码:

static void Main(string[] args)
    {
        int[,] initialArray = new int[3, 5];

        FillArray(initialArray);
        PrintArray(initialArray);
        SumRows(initialArray);
        SumCols(initialArray);
        SumArray(initialArray);
        int rows;
        int cols;

        int userInput = GetNumber();

        if (SearchArray(userInput, initialArray, out rows, out cols))
        {
        Console.WriteLine("your number, " + userInput + " was found at row index " + rows + " and column index " + cols);
        }

        else
        {
        Console.WriteLine("Number not found");
        }
    }

    public static void FillArray(int[,] arrayOfRandoms)
    {
        Random num = new Random();
        for (int r = 0; r < arrayOfRandoms.GetLength(0); r++)
        {
            for (int c = 0; c < arrayOfRandoms.GetLength(1); c++)
            {
                arrayOfRandoms[r, c] = num.Next(15, 97);
            }
        }
    }

    public static void PrintArray(int[,] arrayPrints)
    {
        for (int r = 0; r < arrayPrints.GetLength(0); r++)
        {
            for (int c = 0; c < arrayPrints.GetLength(1); c++)
            {
                Console.Write("{0,3:F0}", arrayPrints[r, c]);
            }
            Console.WriteLine("");
        }
        Console.WriteLine("");
    }

    public static void SumRows(int[,] rowsArray)
    {
        int sumOfRows;
        for (int r = 0; r < rowsArray.GetLength(0); r++)
        {
            sumOfRows = 0;
            for (int c = 0; c < rowsArray.GetLength(1); c++)
            {
                sumOfRows += rowsArray[r, c];
            }
            Console.WriteLine("The total sum for row "+ (r + 1) + " is:  " + sumOfRows + ".");
        }
        Console.WriteLine("");
    }

    public static void SumCols(int[,] columnArray)
    {
        int columnSum;
        for (int c = 0; c < columnArray.GetLength(1); c++)
        {
            columnSum = 0;
            for (int r = 0; r < columnArray.GetLength(0); r++)
            {
                columnSum += columnArray[r, c];
            }
            Console.WriteLine("The total sum for column " + (c + 1) + " is:  " + columnSum + ".");
        }
        Console.WriteLine("");
    }

    public static void SumArray(int[,] arraySum)
    {
        int sumOfArray = 0;
        for (int r = 0; r < arraySum.GetLength(0); r++)
        {

            for (int c = 0; c < arraySum.GetLength(1); c++)
            {
                sumOfArray += arraySum[r, c];
            }
        }
        Console.WriteLine("Total for sum of the Array is:  "  + sumOfArray + "\n");
    }

    public static int GetNumber()
    {
        Console.Write("Please enter a number between 15 and 96:  ");
        int userNumber = int.Parse(Console.ReadLine());

        while (userNumber > 96 || userNumber < 15)
        {
            Console.Write("Number not between 15 and 96.  Try again:  ");
            userNumber = int.Parse(Console.ReadLine());
        }

        return userNumber;
    }


    public static bool SearchArray(int searchedNum, int [,] rArray, out int indexOfRow, out int indexOfColumn)
    {
        indexOfRow = -1;
        indexOfColumn = -1;


        for (int c = 0; c < rArray.GetLength(0); c++)
        {  
        for (int r = 0; r < rArray.GetLength(1); r++)
            {
                indexOfRow = r;
                indexOfColumn = c;

                if (rArray[r, c].Equals(searchedNum))
                {
                    return true;
                }

            }

        }
        return false;

    }

}

【问题讨论】:

  • 它在哪里崩溃,你得到的完整错误是什么?你的预期输出是什么?用上述答案更新您的问题。
  • 您遇到了什么异常?请详细说明它是如何崩溃的。
  • 我更新了这个问题现在应该更详细了,我将提供更多需要的细节。请帮忙。
  • 异常被称为IndexOutOfRange Exception

标签: c# arrays for-loop multidimensional-array parameters


【解决方案1】:

SearchArray,你的代码

for (int c = 0; c < rArray.GetLength(0); c++)
{  
    for (int r = 0; r < rArray.GetLength(1); r++)
    {
        indexOfRow = r;
        indexOfColumn = c;

        if (rArray[r, c].Equals(searchedNum))

错了——具体来说,您可能在

处切换了行和列
for (int c = 0; c < rArray.GetLength(0); c++)
{  
    for (int r = 0; r < rArray.GetLength(1); r++)
    {

您可以通过切换for (int c = 0 ...for (int r = 0 ... 中的变量名来修复它。到目前为止,例如,当尝试访问第 3 行和第 5 列时,您的程序实际上正在访问第 5 行和第 3 列,从而导致 IndexOutOfRange 异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 2015-04-20
    相关资源
    最近更新 更多