在二维数组中搜索值然后返回 true 或 false

Searching for value within 2d Array then returning true or false

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

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

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

这是我的代码:

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;

    }

}

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 异常。