首页 文章

在2d Array中搜索值,然后返回true或false

提问于
浏览
-2

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

SearchArray()方法将返回一个bool,以指示是否找到了搜索的数字 . 它的参数应该是:一个整数,它是要搜索的数字,一个要搜索的二维数组,一个表示行索引的out引用整数参数和一个表示列索引的out引用整数参数方法将使用第一个参数和二维数组,并将在数组中搜索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;

    }

}

1 回答

  • 0

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

相关问题