0

私が苦労しているばかげた質問があります。

ネストされた for ループを使用して C# 多次元配列をステップ実行しようとしていますが、必要な結果が得られず、コードの愚かな問題だと考えています。

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 で見つかった結果」です。

私は何を間違えましたか?

4

4 に答える 4

0

Also check the name on the parameter you use, sometimes you call it iLoop, others iloop. Be consistent! :)

I guess the answer is always in row 1, you just pring the i value, print also the j value jLoop.

于 2013-05-20T14:18:29.403 に答える
0

このコード スニペットは、一致が見つかった後も検索を続けることに注意してください。したがって、実際に見つけているのは、一致したテキストの最後の位置です。

余談ですが、一致する のみを報告する代わりに、一致するとiLoopの両方を報告することもできます。または、単一のインデックスを次のようにレポートできます。iLoopjLoopiLoop * sounds.GetLength(0) + jLoop

于 2013-07-14T18:55:03.147 に答える