我的程序在gitbash中评分后说编译错误

时间:2017-03-19 11:12:06

标签: java turtle-graphics

我正在编写一个Java程序,我必须让乌龟移动到西部,东部,北部和南部,但到目前为止我还没有让乌龟向所需的方向移动。

public class Assignment7 {
    // TODO - you need to implement this. Move the given turtle to the West, n times
    public static void moveTurtleWest(Turtle t, int n)
    {

    }

    // TODO - you need to implement this. Move the given turtle to the East, n times
    public static void moveTurtleEast(Turtle t, int n)
    {
        for(int i=0; i <n;i++ ){ 
        t.moveEast();
    }

    // TODO - you need to implement this. Move the given turtle to the North, n times
    public static void moveTurtleNorth(Turtle t, int n)
    {
        for(int i=0; i <n;i++ ){ 
        t.moveNorth();
    }

    // TODO - you need to implement this. Move the given turtle to the South, n times
    public static void moveTurtleSouth(Turtle t, int n)
    {
        for(int i=0; i <n;i++ ){ 
        t.moveSouth(); 
    }

    // TODO - you need to implement this. Move the turtle to the asked position, by calling MoveXXX etc
    public static void moveTurtleTo(Turtle t, int x, int y)
    {
        moveTurtleTo(turtle.xPos,turtle.yPos);
    }

    public static void main(String[] args) {
        // you can use this as you wish to test or exercise your function. Not graded.
        Turtle t=new Turtle();
        moveTurtleTo(t,15,16);
        System.out.println(t);  
    }
}

1 个答案:

答案 0 :(得分:0)

我猜你正在钓鱼:

public class Assignment7 {

    public static void moveTurtleWest(Turtle t, int n)
    {
        for (int i = 0; i < n; i++) { 
            t.moveWest();
        }
    }

    public static void moveTurtleEast(Turtle t, int n)
    {
        for (int i = 0; i < n; i++) { 
            t.moveEast();
        }
    }

    public static void moveTurtleNorth(Turtle t, int n)
    {
        for (int i = 0; i < n; i++) { 
            t.moveNorth();
        }
    }

    public static void moveTurtleSouth(Turtle t, int n)
    {
        for (int i = 0; i < n; i++) { 
            t.moveSouth();
        }
    }

    public static void moveTurtleTo(Turtle t, int x, int y)
    {
        int delta_x = x - turtle.xPos;
        int delta_y = y - turtle.yPos;

        if (delta_x > 0) {
            moveTurtleEast(t, delta_x);
        } else if (delta_x < 0) {
            moveTurtleWest(t, -delta_x);
        }

        if (delta_y > 0) {
            moveTurtleNorth(t, delta_x);
        } else if (delta_y < 0) {
            moveTurtleSouth(t, -delta_x);
        }
    }

    public static void main(String[] args) {

        Turtle t = new Turtle();

        moveTurtleTo(t, 15, 16);
    }
}

在发布到SO时要明确你的情况,你可能会更快地得到更好的答案。