Я пишу программу Java, в которой я должен заставить черепаху двигаться на запад, но то, что у меня до сих пор не заставляет черепаха двигаться в желаемом направлении. < /p>
public static void moveTurtleWest(Turtle t, int n)
{
for(; n > t.getX();n-- ){
t.moveWest();
}
}
< /code>
Вот остальная часть моего кода: < /p>
package assignment;
import java.text.MessageFormat;
import java.util.Scanner;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.time.LocalDate;
/**
* This class represents a turtle, like a Logo-like turtle, which keeps its position.
* The turtle keeps its x and y position, and has methods moveNorth() etc which change it
* Assume normal math coordinates and map convention, so as you move up (North) y increases, and as you move East x increases.
*
*/
class Turtle {
// HINT - you may want to have variables to keep the position. Keep these variables private,
private int x,y;
// TODO - The empty constructor initializes position to 10,10
public Turtle() {
this.x = 10;
this.y = 10;
}
public int getX() {
// TODO - implement this
return this.x;
}
public int getY() {
// TODO - implement this
return this.y;
}
public void moveNorth() {
// TODO - implement this. this increments the y coordinate by one unit
this.y += 1;
}
public void moveSouth() {
// TODO - implement this. this decrements the y coordinate by one unit
this.y -=1;
}
public void moveEast() {
// TODO - this increments the x coordinate by one unit
this.x +=1;
}
public void moveWest() {
// TODO - this decrements the x coordinate by one unit
this.x -=1;
}
public String toString() {
return "Turtle[x="+getX()+", y="+getY()+"]";
}
public boolean equals(Turtle t)
{
// TODO - you need to implement this
// two turtles are equal if their X and Y coordinates are equal.
if (t == null) {
return false;
}
if (!(t instanceof Turtle)) {
return false;
}
return (x == ((Turtle) t).x && y == ((Turtle) t).y);
}
public void move(String direction)
{
// TODO - you need to implement this
// move to the right direction; direction can be North, South, East, West
moveEast();
}
}
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)
{
for(; n > t.getY();n-- ){
t.moveWest();
}
}
// TODO - you need to implement this. Move the given turtle to the East, n times
public static void moveTurtleEast(Turtle t, int n)
{
}
// TODO - you need to implement this. Move the given turtle to the North, n times
public static void moveTurtleNorth(Turtle t, int n)
{
}
// TODO - you need to implement this. Move the given turtle to the South, n times
public static void moveTurtleSouth(Turtle t, int n)
{
}
// 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)
{
}
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);
}
}
< /code>
Вот код, который выполняется, когда я запускаю этот метод в Git Bash: < /p>
public void testMoveWest()
{
Turtle t=new Turtle();
t.moveWest();
t.moveWest();
t.moveWest();
t.moveWest();
Assert.assertEquals(6, t.getX());
}
@Grade(points=10)
@Test
< /code>
Любая помощь ценится, и очки будут вознаграждены.
заранее! < /p>
Подробнее здесь: https://stackoverflow.com/questions/400 ... -direction