不正确'如果'声明造成了错误的结果

时间:2014-12-07 00:09:44

标签: c# if-statement switch-statement boolean-expression

在调试器中,我确认当它应该是的时候,moveAmount等于1,但是当我到达类中的if语句时,它执行第二个if语句,它需要movingAmount为10。

My Point对象,位置,最终加上一个额外的,所以Y坐标结果是11而不是10,我认为这可能是问题的一部分。

类别:

public partial class Robot
{
    public string direction = "north";
    private Point position = new Point(0, 0);
    private int movementAmount = 1;

    public Robot() {}

    public void moveRobot()
    {
        if (direction == "north" || direction == "North" & movementAmount == 1)
        {
            position.Y += movementAmount;
        }
        if (direction == "north" || direction == "North" & movementAmount == 10)
        {
            position.Y += movementAmount;
        }
        if (direction == "east" || direction == "East" & movementAmount == 1)
        {
            position.X += movementAmount;
        }
        if (direction == "east" || direction == "East" & movementAmount == 10)
        {
            position.X += movementAmount;
        }
        if (direction == "south" || direction == "South" & movementAmount == 1)
        {
            position.Y -= movementAmount;
        }
        if (direction == "south" || direction == "South" & movementAmount == 10)
        {
            position.Y -= movementAmount;
        }
        if (direction == "west" || direction == "West" & movementAmount == 1)
        {
            position.X -= movementAmount;
        }
        if (direction == "west" || direction == "West" & movementAmount == 10)
        {
            position.X -= movementAmount;
        }
    }

    public Point Position
    {
        get
        {
            return position;
        }
    }

    public int MovementAmount
    {
        get
        {
            return movementAmount;
        }
        set
        {
            movementAmount = value;
            if (movementAmount != 1 & movementAmount != 10)
            {
                throw new ArgumentOutOfRangeException("Must be 1 or 10");
            }
        }
    }
 }

}

主程序:

public partial class frmSimpleRobot : Form
{
    public frmSimpleRobot()
    {
        InitializeComponent();
    }

    Robot arrow = new Robot();

    private void btnGoOne_Click(object sender, EventArgs e)
    {
        arrow.MovementAmount = 1;
    }

    private void btnGoTen_Click(object sender, EventArgs e)
    {
        arrow.MovementAmount = 10;
    }

    private void btnNorth_Click(object sender, EventArgs e)
    {
        arrow.direction = "north";
        arrow.moveRobot();
        lblRobotPos.Text = "(X=" + arrow.Position.X.ToString() + ", " + "Y=" + arrow.Position.Y.ToString() + ")";
    }

    private void btnEast_Click(object sender, EventArgs e)
    {
        arrow.direction = "east";
        arrow.moveRobot();
        lblRobotPos.Text = "(X=" + arrow.Position.X.ToString() + ", " + "Y=" + arrow.Position.Y.ToString() + ")";
    }

    private void btnSouth_Click(object sender, EventArgs e)
    {
        arrow.direction = "south";
        arrow.moveRobot();
        lblRobotPos.Text = "(X=" + arrow.Position.X.ToString() + ", " + "Y=" + arrow.Position.Y.ToString() + ")";
    }

    private void btnWest_Click(object sender, EventArgs e)
    {
        arrow.direction = "west";
        arrow.moveRobot();
        lblRobotPos.Text = "(X=" + arrow.Position.X.ToString() + ", " + "Y=" + arrow.Position.Y.ToString() + ")";
    }


}

}

请帮助吗?

1 个答案:

答案 0 :(得分:4)

您应该将||个表达式括在括号中。 &运算符(以及&&运算符(实际应该使用的)具有更高的优先级,因此只要方向字符串匹配,整个表达式就为真。

那就是说,那段代码只是疯狂(没有冒犯的意图:))。它很难阅读,效率低下,容易出错(你的问题就是这样)。尝试这样的事情:

public void moveRobot()
{
    switch (direction)
    {
    case "north":
    case "North":
        position.Y += movementAmount;
        break;
    case "east":
    case "East":
        position.X += movementAmount;
        break;
    case "south":
    case "South":
        position.Y -= movementAmount;
        break;
    case "west":
    case "West":
        position.X -= movementAmount;
        break;
    }
}

更好的方法是将direction设为enum而不是string,以确保始终获得有效值。