首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Karel Midpoint练习中使用Getters和Setters (Java)

在Karel Midpoint练习中使用Getters和Setters (Java)
EN

Stack Overflow用户
提问于 2016-01-15 08:07:26
回答 1查看 137关注 0票数 1

我编写这段代码的目的是使用比课程预期更多的Java元素。但是我很难让它在所有4个方向都能工作。getters和setter被嵌入到让Karel移动的方法中。如果我能得到任何帮助,让这条鳕鱼工作,那就太好了。

代码语言:javascript
复制
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();
    }

}
EN

回答 1

Stack Overflow用户

发布于 2016-03-13 17:34:20

你能详细说明一下你的代码应该做什么吗?如果你想做的就是在每面墙的中点放置一个蜂鸣器,我会做一个单独的方法来实现这一点。如果我的解决方案不能回答您的问题,请提前道歉!

代码语言:javascript
复制
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();

    }


}*/
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34802138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档