简体   繁体   English

if语句中的三元运算符,不会产生任何结果

[英]Ternary operator inside an if-statement that results in nothing

Ok, I'm probably going about this all wrong, but I can't seem to find a good solution, so any pointers would help. 好的,我可能会解决所有这些错误,但是我似乎找不到一个好的解决方案,因此任何指针都将有所帮助。 I have the following statement in my code: 我的代码中包含以下语句:

if (!mapDict.ContainsKey(_thisRoom.Item1))
        {

            MapGraphItem roomMGI = new MapGraphItem();


            var rndOrderRooms = roomList.OrderBy(i => rnd.Next());
            foreach (MapGraphItem room in rndOrderRooms)
            {
                if (!room._flags.IsFlagSet(GlobalValues.MapTileType.start) && !room._flags.IsFlagSet(GlobalValues.MapTileType.exit)
                    && ((_thisRoom.Item2 == 'N') ? room._north : (_thisRoom.Item2 == 'S') ? room._south : (_thisRoom.Item2 == 'E') ? room._east : room._west)
                    && ((mapOpenings.Count < 4) : !room._flags.IsFlagSet(GlobalValues.MapTileType.deadend) ? *ignore this section*))
                {
                    roomMGI = room;
                    earlyRooms.Add(room);
                    goto Exit;
                }
            }

            Exit:
            //MapGraphItem _room = earlyRooms[rnd.Next(0, earlyRooms.Count)];

            GameObject _roomGO = (GameObject)Instantiate(roomMGI.gameObject, _thisRoom.Item1, Quaternion.identity);
            roomMGI._position = GlobalValues.MapTilePos.Early;
            mapDict.Add(_roomGO.transform.position, roomMGI);

            _mapUsed++;

            if (roomMGI._north) _n = true;
            if (roomMGI._south) _s = true;
            if (roomMGI._east) _e = true;
            if (roomMGI._west) _w = true;

            Debug.Log ("Early room added at: " + _thisRoom.Item1.ToString() + " N? " + (_n ? "yes" : "no") + " S? " + (_s ? "yes" : "no") + 
                " E? " + (_e ? "yes" : "no") + " W? " + (_w ? "yes" : "no"));

            GetRoomOpenings(_roomGO.transform.position, _n, _s, _e, _w);

            _tiles--;
        }

Basically, the *ignore this section* area means I want nothing to happen. 基本上,“ *ignore this section*区域意味着我什么也不想做。

In other words, assuming the first two lines of the if are correct, if the .Count is less than four, there's an additional condition. 换句话说,假设if的前两行是正确的,如果.Count小于四,则存在附加条件。 If .Count is four or more, that condition is not needed. 如果.Count为四个或更多,则不需要该条件。

I can't set it to the opposite, just room._flags.IsFlagSet(GlobalValues.MapTileType.deadend) because I don't want it to be forced to be a dead end. 我不能将其设置为相反,仅是room._flags.IsFlagSet(GlobalValues.MapTileType.deadend)因为我不希望它被强制成为死胡同。 I just want to make sure that it's NOT a dead end if there are fewer than four mapOpening left. 我只想确保如果mapOpening少于四个,那不是死路mapOpening

You can rewrite 你可以重写

((mapOpenings.Count < 4)) ? !room._flags.IsFlagSet(GlobalValues.MapTileType.deadend) : *ignore this section*)

as

((mapOpenings.Count < 4) || !room._flags.IsFlagSet(GlobalValues.MapTileType.deadend))

The below code both answers the question and refactors your code, which is in desperate need of improvement in the legibility department (as noted by many people in the comments). 下面的代码既可以回答问题,又可以重构您的代码,这在易读性部门中迫切需要改进(正如很多人在评论中指出的那样)。

if (room._flags.IsFlagSet(GlobalValues.MapTileType.start) {
 return;
}

if (room._flags.IsFlagSet(GlobalValues.MapTileType.exit) {
 return;
}

bool dirFlag = false;
if (_thisRoom.Item2 == 'N') {
 dirFlag = room._north;
}else if (_thisRoom.Item2 == 'S') {
 dirFlag = room._south;
}else if (_thisRoom.Item2 == 'E') {
 dirFlag = room._east;
}else {
 dirFlag = room._west;
}

if (!dirFlag) {
 return;
}

if (mapOpenings.Count < 4 && room._flags.IsFlagSet(GlobalValues.MapTileType.deadend)) {
 return;
}

roomMGI = room;
earlyRooms.Add(room);
goto Exit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM