2017-03-19 7 views
-1

Ich schreibe ein Java-Programm, in dem ich die Schildkröte bewegen müssen, um West, Ost, Nord und Süd zu bewegen, aber was ich bisher habe nicht die Schildkröte zu bewegen in der gewünschten Richtung.Mein Programm sagt Compiler Fehler nach der Einstufung in Gitbash

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); 
    } 
} 
+0

Warum haben Sie eine moveTurtleTo() -Methode innerhalb einer Methode namens moveTurtleTo()? zeigen Sie Ihre überladene Methode und zeigen Sie die Turtle-Klasse – abcOfJavaAndCPP

+0

Compiler-Fehler unterscheiden sich von unerwünschtem Verhalten. Könnten Sie bitte die Kompilierungsfehler veröffentlichen? – efekctive

+0

wie soll es bitte sein? – oneirion

Antwort

0

Ich vermute, Sie fischen für so etwas wie:

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

über Ihre Situation klar sein, wenn zu SO Buchung und Sie werden wahrscheinlich eine bessere Antwort schneller.

Verwandte Themen