在2-D数组中搜索特定数字的方法,如果找到则返回布尔值true

时间:2015-04-21 20:34:16

标签: c# arrays for-loop methods parameters

我正在编写一个创建二维数组的程序,用随机数填充它,然后提示用户输入一个数字并在二维数组中搜索该数字。

我在最后一个丢失的方法旁边完成了整个程序。 我应该让这个方法返回一个bool来表明是否找到了找到的号码。我应该将行和列参数初始化为-1并使用此方法使用第一个参数和2-d数组参数来搜索数组中的数字。如果找到了数字,我将把行和列参数分配给找到它的行和列索引,并立即停止搜索数组。

非常感谢searchArray()方法的任何建议。谢谢!

以下是我到目前为止的最后一个方法中填写错误的代码:

static void Main(string[] args)
    {

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


        FillArray(randomNumArray);
        PrintArray(randomNumArray);
        SumRows(randomNumArray);
        SumCols(randomNumArray);
        SumArray(randomNumArray);
        GetNumber();

    }

    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)
    {
        bool itsTrue == false;
        for (int c = 0; c < searchableArray.GetLength(0); c++)
        {
            for (int r = 0; r < searchableArray.GetLength(1); r++)
            {
                if (searchableArray[r, c] == soughtOutNum)
                {
                    return itsTrue == true;
                    break;
                }
            }

        }
        Console.WriteLine("");
    }

}

}

2 个答案:

答案 0 :(得分:0)

您的代码有四个我可以看到的问题。

首先,您的两个参数被标记为out参数,但您还没有为它们填充值。考虑多个返回值之类的参数:而不是调用者将函数传递给某些东西,而是将其传递回去。由于调用此函数的人可能会依赖于那些函数中的值,因此您必须明确地为它们赋值。这可能会给你编译错误。

我首先在函数开头为每两个输出参数分配-1,当你找到你要搜索的数字(内部if语句)时,覆盖那些-1和#39 ; s有真正的答案(提示:你怎么知道你在哪个栏目和行?)

其次,itsTrue有点奇怪。在C#(以及许多其他语言)中,我们使用单个=进行分配,使用双==进行比较。如果你解决了这个问题,它应该可行,并给你正确的答案。但是,从代码清晰度的角度来看,为什么你需要变量呢?除了作为返回值之外,它从未被使用过......为什么当你找到你正在寻找的号码时,你不能直接返回true

第三,你的方法不能保证返回一个值:如果它从未找到一个数字会怎么样?最终,你不会在两个for循环之外,并且你将达到底部的Console.WriteLine(""); ......但是在这种情况下你会回报什么?

最后,当你调用方法时,你需要让C#知道在哪里粘贴那些out参数的值。现在,当您尝试拨打SearchNumber(10)时,它无法找到只有一个参数的方法。只有一个有3个。您应该在可以存储行和列的位置声明变量,然后将其传递给,如下所示:

int row, col;
SearchNumber(10, out row, out col);

答案 1 :(得分:0)

如果我理解正确的话,我认为你离解决方案不是那么远。你在寻找这样的东西吗?

    public static bool SearchArray(int soughtOutNum, int[,] searchableArray, out int rowIndex, out int colsIndex)
    {
        rowIndex = -1;
        colsIndex = -1;
        // Assuming your c is column and r is row
        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;
                    //Console.WriteLine("found number");
                    return true;
                }
            }
        }
        //Console.WriteLine("Number not found");
        return false;
    }

更新以回复评论: 我在没有Visual Studio的情况下对其编码,因此请注意拼写错误。

static void Main(string[] args)
{

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


    FillArray(randomNumArray);
    PrintArray(randomNumArray);
    SumRows(randomNumArray);
    SumCols(randomNumArray);
    SumArray(randomNumArray);
    int row;
    int column;
    int search = GetNumber();
    if (SearchArray(search, randomNumArray, out row, out column))
    {
        Console.WriteLine("found " + search + " at row " + row + " col " + column);
    }
    else
    {
        Console.WriteLine("Number not found");
    }

}

更新2: 最后一种方法也有错误 你已经像这样构造了你的数组:randomNumbersArray [row,column]

在所有方法中, r 来自 GetLength(0) c GetLength(1)。但是在最后一个方法中你切换了你的 r是GetLength(1) c是GetLength(0)。所以你在访问数组时切换数字,本质上调用randomNumbersArray [column,row]。当c_max!= r_max。

时,这会给你一个错误