2014-12-07 91 views
0

在调试器中,我已经验证了moveAmount在应该是时等于1,但是当我到达类中的if语句时,它会执行第二个if语句movementAmount为10.错误的'if'语句导致错误的结果

我的点对象,位置,最后加上一个额外的,所以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() + ")"; 
    } 


} 

}

援助吗?

+0

当'direction =='north''时,'movementAmount'的值不会有太大影响。使用'()' – 2014-12-07 00:14:11

+0

@AlexBell:“方向==”北方“**”和“方向==”北方“”如何都是真的? – 2014-12-07 00:15:58

+0

最好的方法是使用'else if'或'switch'语句,如下所示。问候, – 2014-12-07 00:20:39

回答

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; 
    } 
} 

更妙的是使directionenum代替string,以确保您始终获得一个有效的值。

+3

'switch(direction.ToLower())'? – 2014-12-07 00:23:58

+2

以上内容与OP的原始代码更一致。请注意,如果您要使用小写字母,则您需要调用'direction.ToLowerInvariant()'。但是它是浪费的,因为它(相对昂贵)只是为了比较的目的而创建一个新的'string'实例,而'switch'语句可以很好地处理这些选项。另外,当原始代码没有时,调用'ToLower()'将允许像“NoRTH”等等。是否合理无法根据给定的代码来确定。使用一个'enum'(我建议)会使整个问题都没有意义。 – 2014-12-07 00:28:12

+1

使用枚举肯定更好,因为你总是说有效的值。 – DRapp 2014-12-07 00:31:34