2016-01-15 137 views
1

我編寫了這段代碼,使用了比本課程更多的Java元素。但我很難讓它在所有4個方向上工作。吸氣劑和吸附劑嵌入使卡雷爾移動的方法中。如果我能得到任何幫助,使這個鱈魚工作,這將是偉大的。在Karel Midpoint練習中使用Getters和Setter練習(Java)

import stanford.karel.*; 

public class MidpointFindingKarel extends SuperKarel { 


public void run(){ 
    while(facingEast()){ 
     moveEast(); 
    } 
    while(facingNorth()){ 
     moveNorth(); 
    } 
    while(facingWest()){ 
     moveWest(moveEast()); 
    }  
    while(facingSouth()){ 
     moveSouth(moveNorth()); 
    } 
} 

    private int moveEast(){ 
     int width = 0; 
     while(frontIsClear()){ 
      width++; 
      move(); 
     } 
     turnLeft(); 
     width /= 2; 
     return width; 
    } 

    private int moveNorth(){ 
     int height = 0; 
     while(frontIsClear()){ 
      height++; 
      move(); 
     } 
     turnLeft(); 
     height /= 2; 
     return height; 
    } 

    private void moveWest(int _width){ 
     for(int _w = 0; _w < _width; _w++){ 
      move(); 
     } 
     turnLeft(); 
    } 

    private void moveSouth(int _height){ 
     for(int _h = 0; _h < _height; _h++){ 
      move(); 
     } 
     turnLeft(); 
    } 

} 

回答

0

請問您能說明一下,您的代碼應該做什麼? 如果你想要做的就是在每個牆壁的中點放置一個蜂鳴器,那麼我會做一個單獨的方法來做到這一點。 如果我的解決方案沒有回答您的問題,請提前抱歉!

import stanford.karel.*; 

public class MidpointFindingKarel extends SuperKarel { 
public void run(){ 
    while(facingEast()){ 
     moveEast(); 
    } 
    while(facingNorth()){ 
     moveNorth(); 
    } 
    while(facingWest()){ 
     moveEast(); // I did not understand what "moveWest(moveEast());" was for. 
    }  
    while(facingSouth()){ 
     moveNorth();// I did not understand what "moveSouth(moveNorth())" was for. 
    } 
} 

    private void moveEast(){ 
     int width = 0; 
     while(frontIsClear()){ 
      width++; 
      move(); 
     } 
     goBackPutBeeperReturn(width); // goes back half of the "width", puts beeper, returns. 
     turnLeft(); 
     // width /= 2; 
     // return width; 
    } 

    private void moveNorth(){ 
     int height = 0; 
     while(frontIsClear()){ 
      height++; 
      move(); 
     } 
     goBackPutBeeperReturn(height); 
     turnLeft(); 
     // height /= 2; 
     // return height; 
    } 


    //Pre-condition: Karel has finished going along a wall and has its length stored somewhere. 
    //Post-condition: Karel has put a beeper in the midpoint and is back to the end of that wall 
    private void goBackPutBeeperReturn(int wallLength){ 
      turnAround(); 
      for(int loop = 0 ; loop < wallLength/2 ; loop++){ 
      move(); 
      } 
      putBeeper(); 
      turnAround(); 
      for(int loop = 0 ; loop < wallLength/2 ; loop++){ 
       move(); 
     }}} 

     /* 
    private void moveWest(int _width){ 
     for(int _w = 0; _w < _width; _w++){ 
      move(); 
     } 
     turnLeft(); 
    } 

    private void moveSouth(int _height){ 
     for(int _h = 0; _h < _height; _h++){ 
      move(); 
     } 
     turnLeft(); 

    } 


}*/