简体   繁体   English

创建Bug类,该类将移回原始位置

[英]Creating bug class, which moves back to original position

I'm very new to Java and I'm confused on how to create a method that goes back to original position, going through each step and turn. 我是Java的新手,我对如何创建返回到原始位置的方法,每个步骤和每个步骤都感到困惑。

This is an assignment for school and essentially, I have to create a Bug class, which moves 1 unit of distance on a horizontal line, turns, reverses, and moves back to original position. 这是学校的一项工作,从本质上讲,我必须创建一个Bug类,该类在一条水平线上移动1单位距离,然后转弯,反转并移回原始位置。

Here's the code I have so far: 这是我到目前为止的代码:

public class Bug
{
    private int position;
    private String direction;

    public void initialPosition(int position ) {
        position=0;
    }

    public void initialDirection(String direction) {
        direction="right";
    }

    public void setPosition (int position) {
        this.position = position;
    }

    public int getPosition() {
        return position;
    }

    public void setDirection (String direction) {
        this.direction = direction;
    }

    public String getDirection() {
        return direction;
    }

    public void move() {
        position+=1;
    }   

    public void reverseDirection() {
        if (direction=="right") {
            direction="left";
        }
        else {
            direction="right";
        }
    }

    public String toString() {
        return "Position: " + position + " & Direction: " + direction;
    }
}

Any tips on code will be super appreciated as well. 任何有关代码的技巧也将受到高度赞赏。 Thanks! 谢谢!

In your move() function, get the current direction and then use an if statement so that 在您的move()函数中,获取当前方向,然后使用if语句,以便

if (direction.equals("right")) {
    position+=1;
} 
else {
    position-=1;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次我滚动/平移/拖动时,相机都会移回原始位置 - Camera moves back to original position everytime I scroll/pan/drag 调整窗口大小时,组件将移动到原始位置 - component moves to original position when window is resized Libgdx 将运动图像返回到原始位置 - Libgdx return moving image back to original position 转换回原始类时出现ClassCastException错误 - ClassCastException error when casting back to original class Java revalidate(); 将已重新定位的对象移回原始位置 - Java revalidate(); moves Objects that have been re-positioned, back to the original location 刷新时,Android自定义视图会移回原始位置 - Android Custom View move back to original position when refreshing 返回到原始开始位置,而无需退出应用程序并再次打开它 - Go back to original start position without Quitting the application and opening it again JNA / WinAPI。 模拟鼠标点击移动鼠标 cursor 并且不会将其返回到开始 position - JNA / WinAPI. Simulating mouse click moves mouse cursor and doesn't return it back to the start position 在Android中使用原始类的意图后,将意图传递回原始类的正确方法是什么? - What is the correct way to pass an intent back to the original class after using the intent from the original class in Android? 创建受原始集合更改影响的多个集合的并集 - Creating a Union of multiple sets which is affected by changes in original sets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM