如何使用開關嗨新來編程的人...是否有可能將此更改爲開關語句而不是if if if if if請請指向正確的方向如何使用開關,而不是如果其他如果
int dir = get_dir();
if (dir == 0) // facing north (up)
{
setX(x); // goes to x position
setY(y - 1); // goes to y -1 position
}
else if (dir == 1) // facing east (right)
{
setX(x - 1); // goes to x -1 position
setY(y); // goes to y position
}
else if (dir == 2) // facing south (down)
{
setX(x); // goes to x position
setY(y - 1); // goes to y -1 position
}
else if (dir == 3) // facing west (left)
{
setX(x - 1); // goes to x -1 position
setY(y); // goes to y position
}
是的,這是可能的。你可以使用常量而不是神奇的數字,如SOUTH = 2 .. – nachokk
你已經到了一半。第一步:瞭解開關盒。第二步:實施它。 – keyser
你可以簡化你的if語句。如果'dir == 0'或'dir == 2',你正在做同樣的事情。 '1'和'3'相同 –