不正确的“if”语句导致错误的结果

本文关键字:错误 结果 if 不正确 语句 | 更新日期: 2023-09-27 18:33:26

在调试器中,我已经验证了 movementAmount 在应该等于 1 时,但是当我到达类中的 if 语句时,它会执行第二个 if 语句,该语句要求 movementAmount 为 10。

我的 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() + ")";
    }

}

}

请帮忙?

不正确的“if”语句导致错误的结果

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

也就是说,该代码只是疯狂的(无意冒犯:))。它难以阅读、效率低下且容易出现错误(您的问题案例)。请尝试以下操作:

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,以确保您始终获得有效的值。