gpt4 book ai didi

Java 编程 : Turtle not moving in desired direction

转载 作者:行者123 更新时间:2023-11-29 04:38:58 25 4
gpt4 key购买 nike

我正在编写一个 Java 程序,其中我必须让乌龟向西移动,但到目前为止我没有让乌龟向所需方向移动。

public static void moveTurtleWest(Turtle t, int n)
{
for(; n > t.getX();n-- ){
t.moveWest();
}
}

这是我的其余代码:

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

这是我在 git Bash 中运行此方法时正在执行的代码:

public void testMoveWest()
{
Turtle t=new Turtle();
t.moveWest();
t.moveWest();
t.moveWest();
t.moveWest();

Assert.assertEquals(6, t.getX());
}

@Grade(points=10)
@Test

感谢任何帮助,并将奖励积分。提前致谢!

最佳答案

可能是正确的 for 循环:

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

关于Java 编程 : Turtle not moving in desired direction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40055270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com