3

次の関数がありますが、break ステートメントを使用しているにもかかわらず、配列内で一致が見つかった後に停止しているようには見えません。

private function CheckMatch() {

// _playersList is the Array that is being looped through to find a match

            var i:int;
            var j:int;

            for (i= 0; i < _playersList.length; i++) {

                    for (j= i+1; j < _playersList.length; j++) {
                        if (_playersList[i] === _playersList[j]) {
                            trace("match:" + _playersList[i] + " at " + i + " is a match with "+_playersList[j] + " at " + j);

                            break;

                            } else {
                            // no match
                            trace("continuing...")

                            }
                        }
                    }

                }
4

4 に答える 4

11

Ahh...I see.

Used a label, now it works:

private function CheckMatch() {

// _playersList is the Array that is being looped through to find a match

        var i:int;
        var j:int;

     OuterLoop:   for (i= 0; i < _playersList.length; i++) {

                for (j= i+1; j < _playersList.length; j++) {
                    if (_playersList[i] === _playersList[j]) {
                        trace("match:" + _playersList[i] + " at " + i + " is a match with "+_playersList[j] + " at " + j);

                        break OuterLoop;

                        } else {
                        // no match
                        trace("continuing...")

                        }
                    }
                }

            }
于 2010-01-12T17:03:59.373 に答える
2

false に初期化された found という bool 変数を追加します。

からループ条件を変更します

i < _playersList.length

i < _playersList.length && !found

次に、休憩の前に、found = true に設定します

于 2010-01-12T17:00:33.963 に答える
1

break一度に 1 つのループ (またはスイッチ) だけを分割します。

于 2010-01-12T16:58:23.233 に答える
0

コードの少ない別のソリューションがあると思います:

private function checkMatch():void {
    for (var i : int = 0; i < _playerList.length-1; i++) {
        if (_playerList.indexOf(_playerList[i], i+1) > i) {
            break;
        }
    }
}
于 2013-01-02T10:45:31.077 に答える