我正在编写一个创建二维数组的程序,用随机数填充它,然后提示用户输入一个数字并在二维数组中搜索该数字 .
我把整个程序放在最后一个方法旁边,我迷失了 . 我应该让这个方法返回一个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 回答
您的代码有四个我可以看到的问题 .
首先,您的两个参数被标记为
out
参数,但您还没有将其传回 . 由于调用此函数的人可能会依赖于那些函数中的值,因此您必须明确地为它们赋值 . 这可能会给你编译器错误 .我首先在函数开头为每个两个输出参数赋值-1,当你找到你要搜索的数字(内部if语句)时,用真实答案覆盖那些-1(提示:你怎么知道你在哪个列和行?)
第二,如果
itsTrue
有点奇怪,你的使用 . 在C#(和许多其他语言)中,我们使用单个=
进行赋值,使用双_1875899进行比较 . 如果你解决了这个问题,它应该可行,并给你正确的答案 . 但是,从代码清晰度的角度来看,为什么你需要变量呢?当你找到你正在寻找的号码时,你直接返回true
.第三,你的方法不会落在两个for循环之外,你将达到底部的那个
Console.WriteLine("");
但是你在这种情况下又回来了什么?最后,当你调用方法时,你需要让C#知道在哪里粘贴那些out参数的值 . 现在,当你尝试调用
SearchNumber(10)
时,它只能_1775905_只有一个3.你应该声明变量,你可以存储行和列,然后传递它们,如下所示:如果我理解正确的话,我认为你离解决方案的距离不是很远 . 你在找这样的东西吗?
Update 回答评论:我在没有Visual Studio的情况下对其编码,因此请注意拼写错误 .
Update 2: 最后一种方法也有错误 .
你已经像这样构造了你的数组:randomNumbersArray [row,column]
在所有方法中,r来自GetLength(0),c是GetLength(1) . 但是在最后一个方法中你切换,你的r是GetLength(1),c是GetLength(0) . 所以你在访问数组时切换数字,本质上调用randomNumbersArray [column,row] . 当c_max!= r_max时,这会给你一个错误 .