有一个愚蠢的问题,我正在努力。嵌套For循环多维数组搜索
我试着去步骤通过C#多维数组使用嵌套的for循环,但我不能得到的结果,我想和IM想它只是一个愚蠢的问题,我的代码。
string search = txtString.Text;
int iLoop;
int jloop;
int iResult = -1;
for (iLoop = 0; iLoop < sounds.GetLength(0) ; iLoop++)
{
for (jloop = 0; jloop < sounds.GetLength(1) ; jloop++)
{
string result;
result = sounds[iLoop,jloop];
if (result == search)
{
iResult = iloop;
}
}
}
if (iResult == -1)
{
MessageBox.Show("Result not found");
}
else
{
MessageBox.Show("Result found at position " + iResult);
}
}
它搜索阵列,如果答案是发现返回一个肯定的结果,但结果位置始终是“在位置1处发现结果”。
我做错了什么?
也许是因为结果总是在第一行? –
声音是如何声明的?声明了什么? –
btw,在你的代码中'位置'的概念对应于'行'的数量,但不是'行,列'对。这是故意的吗? –