首页 文章

搜索填充有随机数的二维数组的方法的参数的构造和传递

提问于
浏览
0

我的程序的最后一个方法有问题 . 我似乎无法找出传递参数的正确方法,或者也许我编写方法的方式是错误的 . 请帮忙!该方法的规定如下:

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

任何有关这方面的帮助非常感谢我已经在这里试了一段时间试图完全弄清楚如果我运行它它到达它要求一个数字然后遇到错误说明索引超出边界 . 这是我到目前为止的代码:

static void Main(string[] args)
    {

        int [,] randomNumArray = new int[3, 5];

        FillArray(randomNumArray);
        PrintArray(randomNumArray);
        SumRows(randomNumArray);
        SumCols(randomNumArray);
        SumArray(randomNumArray);
        int rows, cols;
        int search = GetNumber();

        SearchArray(search, randomNumArray, out rows, out cols);


    }

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

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

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

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

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

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

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

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

        return chosenNumber;
    }


    public static bool SearchArray(int soughtOutNum, int [,] searchableArray, out int rowIndex, out int colsIndex)
    {
        rowIndex = -1;
        colsIndex = -1;

        for (int c = 0; c < searchableArray.GetLength(0); c++)
        {
            for (int r = 0; r < searchableArray.GetLength(1); r++)
            {
                if (searchableArray[r, c] == soughtOutNum)
                {
                    rowIndex = r;
                    colsIndex = c;
                    return true;
                    break;
                }
            }

        }
        return false;
    }

1 回答

  • 0

    您可以将LINQ用于SearchArray .

    public static bool SearchArray(int soughtOutNum, int [,] searchableArray, out int rowIndex, out int colsIndex)
    {
        if(searchableArray.Any(x => soughtOutNum))
        {
            colsIndex = searchableArray.FirstOrDefault(x => x == soughtOutNum).GetLength(0);
            rowIndex= searchableArray.FirstOrDefault(x => x == soughtOutNum).GetLength(1);
        }
        else
        {
            return false;
        }        
    }
    

    这将返回您的第一个匹配列和行 .

相关问题