문제

다음과 같은 기능이 있지만 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...")

                            }
                        }
                    }

                }
도움이 되었습니까?

해결책

아 알 겠어.

라벨을 사용했는데 이제 작동합니다.

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...")

                        }
                    }
                }

            }

다른 팁

초기화 된 발견이라는 부종 var를 추가하여 false로 추가하십시오.

루프 조건을 변경하십시오

i < _playersList.length

에게

i < _playersList.length && !found

그런 다음 휴식을 취하기 전에 찾은 = 참으로 설정하십시오

break 한 번에 하나의 루프 (또는 스위치) 만 분리합니다.

코드가 적은 다른 솔루션이 있다고 생각합니다.

private function checkMatch():void {
    for (var i : int = 0; i < _playerList.length-1; i++) {
        if (_playerList.indexOf(_playerList[i], i+1) > i) {
            break;
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top