gpt4 book ai didi

java - 在 Karel Midpoint 练习中使用 Getter 和 Setter (Java)

转载 作者:太空宇宙 更新时间:2023-11-04 13:02:37 26 4
gpt4 key购买 nike

我编写这段代码是为了使用比类(class)预期更多的 Java 元素。但我很难让它在所有 4 个方向上工作。 getter 和 setter 嵌入到使 Karel 移动的方法中。如果我能得到任何帮助让这个鳕鱼工作那就太好了。

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();
}

}

最佳答案

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

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();

}


}*/

关于java - 在 Karel Midpoint 练习中使用 Getter 和 Setter (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34802138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com