В отладчике я проверил, что moveAmount равен 1, когда он должен быть, но когда я получаю оператор if в классе, он выполняет второй оператор if, который требует movementAmount будет 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() + ")";
}
}
}
помощь, пожалуйста?
Когда 'direction ==" north "', значение 'movementAmount' не имеет большого значения. Используйте '()' –
@AlexBell: Как могут быть истинны оба «направления ==» на север «' ** и ** 'направление ==« Север »? –
Лучше всего использовать либо «else if», либо «switch», как указано ниже. С уважением, –