Switch case for two INT variables

前端 未结 6 1392
生来不讨喜
生来不讨喜 2021-01-12 19:07

Consider the following code :

if (xPoint > 0 && yPoint > 0) {
    m_navigations = Directions.SouthEast;
}
else if (xPoint > 0 && yP         


        
6条回答
  •  遥遥无期
    2021-01-12 19:49

    Similar to other answers but without strings. Just for fun :-)

    public Directions getDirection(int xPoint, int yPoint) {
        int num = 8 * (xPoint == 0 ? 0 : xPoint > 0 ? 1 : 2);
        num += yPoint == 0 ? 0 : yPoint > 0 ? 1 : 2;
        switch (num) {
        case 01:
            return Directions.South;
        case 02:
            return Directions.North;
        case 010:
            return Directions.East;
        case 011:
            return Directions.SouthEast;
        case 012:
            return Directions.NorthEast;
        case 020:
            return Directions.West;
        case 021:
            return Directions.SouthWest;
        case 022:
            return Directions.NorthWest;
        }
        return Directions.None;
    }
    

提交回复
热议问题