我想創建一個模擬機器人功能的代碼。轉動和移動等。我感覺好像我以各種錯誤的方式來處理這個問題......當我寫這篇文章時,我認爲我理解了它的要旨,如果構造函數中的方向是這樣的,那麼如果它轉向新的方向就是這樣。我測試了這一點,當然,我最終得到了一些真正不正確的結果。我絕對相信我實際上並沒有爲我的對象使用這些函數。我可以得到關於如何使用這種代碼的提示嗎?改變點的方向
import java.awt.Point;
public class Robot
{
private int x;
private int y;
private int d;
private int p;
public static final int NORTH = 0;
public static final int SOUTH = 1;
public static final int EAST = 2;
public static final int WEST = 3;
/**
* Constructor for objects of class Robot
* @param theX the x coordinate
* @param theY the y coordinate
* @param theDirection the direction the robot is facing
*/
public Robot(int theX, int theY, int theDirection)
{
x = theX;
y = theY;
d = theDirection;
}
public void turnLeft()
{
if(d == NORTH) {
d = WEST;
}
if(d == WEST) {
d = SOUTH;
}
if(d == SOUTH) {
d = EAST;
}
if(d == EAST) {
d = NORTH;
}
}
public String getDirection()
{
if(d == NORTH) {
return "N";
}
if(d == SOUTH) {
return "S";
}
if(d == WEST) {
return "W";
}
if(d == EAST) {
return "E";
}
return "";
}
}
測試
Robot rob = new Robot(20, 20, Robot.SOUTH);
rob.turnLeft;
System.out.println(rob.getDirection);
這回當我想起它實際上應該返回E.
你turnLeft()被混合p和d。 – Aquillo 2013-04-29 07:54:38
對不起,這是我的代碼,然後我解決了我的問題,然後我就編輯它! – Michael 2013-04-29 07:55:15
等不是方向? – Michael 2013-04-29 07:56:59