在Karel中点练习(Java)中使用getter和setter
我写这段代码是为了使用比本课程预期的更多的Java元素。但是我很难让它在四个方向上都工作。getter和setter嵌入到使Karel移动的方法中。如果我能得到任何帮助,使这个鳕鱼工作,那将是伟大的在Karel中点练习(Java)中使用getter和setter,java,setter,getter,getter-setter,karel,Java,Setter,Getter,Getter Setter,Karel,我写这段代码是为了使用比本课程预期的更多的Java元素。但是我很难让它在四个方向上都工作。getter和setter嵌入到使Karel移动的方法中。如果我能得到任何帮助,使这个鳕鱼工作,那将是伟大的 import stanford.karel.*; public class MidpointFindingKarel extends SuperKarel { public void run(){ while(facingEast()){ moveEast();
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.*;
公共类中点查找Karel扩展了SuperKarel{
公开募捐{
while(facingEast()){
向东移动();
}
while(朝北()){
北移();
}
while(面朝西()){
moveWest(moveEast());
}
而(朝南(){
南迁(北迁());
}
}
二等兵{
整数宽度=0;
while(fronticlear()){
宽度++;
move();
}
左转();
宽度/=2;
返回宽度;
}
私人住宅{
整数高度=0;
while(fronticlear()){
高度++;
move();
}
左转();
高度/=2;
返回高度;
}
私有空间向西移动(内部宽度){
对于(int _w=0;_w<_width;_w++){
move();
}
左转();
}
私人空间向南移动(内部高度){
对于(int h=0;h
您能指定您的代码应该做什么吗?
如果你想做的就是在每面墙的中点放一个传呼机,我会做一个单独的方法。
如果我的解决方案不能回答您的问题,请提前道歉
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();
}
}*/
import stanford.karel.*;
公共类中点查找Karel扩展了SuperKarel{
公开募捐{
while(facingEast()){
向东移动();
}
while(朝北()){
北移();
}
while(面朝西()){
moveEast();//我不明白“moveWest(moveEast());”是什么意思。
}
而(朝南(){
moveNorth();//我不明白“moveSouth(moveNorth())”是干什么的。
}
}
私人住宅(东){
整数宽度=0;
while(fronticlear()){
宽度++;
move();
}
goBackPutBeeperReturn(宽度);//返回“宽度”的一半,放入寻呼机,返回。
左转();
//宽度/=2;
//返回宽度;
}
私人住宅(北区){
整数高度=0;
while(fronticlear()){
高度++;
move();
}
GobackPutterReturn(高度);
左转();
//高度/=2;
//返回高度;
}
//先决条件:卡雷尔已经沿着墙走完了,并把它的长度储存在某个地方。
//后置条件:卡雷尔在中点放了一个传呼机,回到了墙的尽头
专用void goBackPutBeeperReturn(内部墙长度){
扭转();
for(int loop=0;loop
您能指定您的代码应该做什么吗?
如果你想做的就是在每面墙的中点放一个传呼机,我会做一个单独的方法。
如果我的解决方案不能回答您的问题,请提前道歉
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();
}
}*/
import stanford.karel.*;
公共类中点查找Karel扩展了SuperKarel{
公开募捐{
while(facingEast()){
向东移动();
}
while(朝北()){
北移();
}
while(面朝西()){
moveEast();//我不明白“moveWest(moveEast());”是什么意思。
}
而(朝南(){
moveNorth();//我不明白“moveSouth(moveNorth())”是干什么的。
}
}
私人住宅(东){
整数宽度=0;
while(fronticlear()){
宽度++;
move();
}
goBackPutBeeperReturn(宽度);//返回“宽度”的一半,放入寻呼机,返回。
左转();
//宽度/=2;
//返回宽度;
}
私人住宅(北区){
整数高度=0;
while(fronticlear()){
高度++;
move();
}
GobackPutterReturn(高度);
左转();
//高度/=2;
//返回高度;
}
//先决条件:卡雷尔已经沿着墙走完了,并把它的长度储存在某个地方。
//后置条件:卡雷尔在中点放了一个传呼机,回到了墙的尽头
专用void goBackPutBeeperReturn(内部墙长度){
扭转();
for(int loop=0;loop