在二维数组中搜索特定数字的方法,如果找到,将 return 布尔值 true
Method that Searches 2-D array for a specific number and will return a Boolean value of true if found
我正在编写一个程序,创建一个二维数组并用随机数填充它,然后提示用户输入一个数字并在二维数组中搜索该数字。
除了我迷路的最后一个方法之外,我已经完成了整个程序。
我应该使用此方法 return 一个布尔值来指示是否找到了所查找的号码。我应该将行和列参数初始化为 -1 并使用此方法使用第一个参数和二维数组参数来搜索数组中的数字。如果找到数字,我会将行和列参数分配给找到它的行和列索引,并立即停止搜索数组。
非常感谢对 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("");
}
}
}
你的代码有四个问题,我可以看到。
首先,您的两个参数被标记为out
参数,但您还没有为它们填写值。将 out 参数想象成多个 return 值:不是调用者向您的函数传递某些东西,而是将其传回。由于调用此函数的人可能会依赖其中的值,因此您必须明确地为它们提供一个值。这可能会给您带来编译器错误。
我首先在函数的开头为两个输出参数中的每一个分配 -1,当您找到要搜索的数字(内部 if 语句)时,用真正的答案(提示:你怎么知道你在哪一列和哪一行?)
其次,您使用 if itsTrue
有点奇怪。在 C#(和许多其他语言)中,我们使用单个 =
进行赋值,使用双 ==
进行比较。如果你解决了这个问题,它应该可以工作,并给你正确的答案。然而,从代码清晰的角度来看,为什么你需要这个变量呢?除了作为 return 值外,它从未被使用过...当您找到要查找的号码时,为什么不直接 return true
呢?
第三,您的方法不能保证 return 一个值:如果它永远找不到数字会怎样?最终,您将落入两个 for 循环之外,并且您将到达底部的 Console.WriteLine("");
...但是在那种情况下您 return 是什么?
最后,当您调用该方法时,您需要让 C# 知道将那些输出参数的值粘贴到哪里。现在,当您尝试调用 SearchNumber(10)
时,它无法找到只有一个参数的方法。只有一个有 3 个。您应该改为声明可以存储行和列的变量,然后将它们传递进来,如下所示:
int row, col;
SearchNumber(10, out row, out col);
如果我理解正确的话,我认为你离解决方案不远了。您在找这样的东西吗?
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");
}
}
更新二:
最后一个方法也有错误
你已经像这样构建了你的数组:randomNumbersArray[row, column]
在所有方法中 r 来自 GetLength(0) 而 c 是 GetLength(1)。但是在最后一种方法中,你切换了,你的 r 是 GetLength(1) 而 c 是 GetLength(0)。因此,您在访问数组时切换数字,实际上是调用 randomNumbersArray[column, row]。当 c_max != r_max.
时,这会给你一个错误
我正在编写一个程序,创建一个二维数组并用随机数填充它,然后提示用户输入一个数字并在二维数组中搜索该数字。
除了我迷路的最后一个方法之外,我已经完成了整个程序。 我应该使用此方法 return 一个布尔值来指示是否找到了所查找的号码。我应该将行和列参数初始化为 -1 并使用此方法使用第一个参数和二维数组参数来搜索数组中的数字。如果找到数字,我会将行和列参数分配给找到它的行和列索引,并立即停止搜索数组。
非常感谢对 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("");
}
}
}
你的代码有四个问题,我可以看到。
首先,您的两个参数被标记为out
参数,但您还没有为它们填写值。将 out 参数想象成多个 return 值:不是调用者向您的函数传递某些东西,而是将其传回。由于调用此函数的人可能会依赖其中的值,因此您必须明确地为它们提供一个值。这可能会给您带来编译器错误。
我首先在函数的开头为两个输出参数中的每一个分配 -1,当您找到要搜索的数字(内部 if 语句)时,用真正的答案(提示:你怎么知道你在哪一列和哪一行?)
其次,您使用 if itsTrue
有点奇怪。在 C#(和许多其他语言)中,我们使用单个 =
进行赋值,使用双 ==
进行比较。如果你解决了这个问题,它应该可以工作,并给你正确的答案。然而,从代码清晰的角度来看,为什么你需要这个变量呢?除了作为 return 值外,它从未被使用过...当您找到要查找的号码时,为什么不直接 return true
呢?
第三,您的方法不能保证 return 一个值:如果它永远找不到数字会怎样?最终,您将落入两个 for 循环之外,并且您将到达底部的 Console.WriteLine("");
...但是在那种情况下您 return 是什么?
最后,当您调用该方法时,您需要让 C# 知道将那些输出参数的值粘贴到哪里。现在,当您尝试调用 SearchNumber(10)
时,它无法找到只有一个参数的方法。只有一个有 3 个。您应该改为声明可以存储行和列的变量,然后将它们传递进来,如下所示:
int row, col;
SearchNumber(10, out row, out col);
如果我理解正确的话,我认为你离解决方案不远了。您在找这样的东西吗?
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");
}
}
更新二:
最后一个方法也有错误
你已经像这样构建了你的数组:randomNumbersArray[row, column]
在所有方法中 r 来自 GetLength(0) 而 c 是 GetLength(1)。但是在最后一种方法中,你切换了,你的 r 是 GetLength(1) 而 c 是 GetLength(0)。因此,您在访问数组时切换数字,实际上是调用 randomNumbersArray[column, row]。当 c_max != r_max.
时,这会给你一个错误