【发布时间】:2018-09-04 04:20:27
【问题描述】:
我想编写一个执行以下命令的海龟图形程序:
1 :提笔
2 :下笔
3 :右转
4 :左转
5,10 : 前移 10 个空格(替换 10 个空格)
6 :显示 20×20 数组
9 : 数据结束(哨兵)
我写了两个类,但是当我给它命令 3 向右转时看起来它没有!!!。 你能看看它并告诉我可能是什么问题吗? 非常感谢
一级:
import java.util.Arrays;
public class TurtleGraphics
{
private enum Pen{PEN_UP,PEN_DOWN};
private enum Turn{TURN_RIGHT,TURN_LEFT};
private enum Direction{UP,DOWN,RIGHT,LEFT};
private int [][] floor;
private int [] currentPosition;
private Direction direction;
private Pen pen;
public TurtleGraphics(Direction startDirection)
{
floor = new int [20][20];
currentPosition = new int [2];
pen = Pen.PEN_UP;
direction = startDirection;
}
public TurtleGraphics(int [] initialPosition) // removed paramater direction because i did not know how to pass it from class test
{
floor = new int [20][20];
currentPosition = new int [2];
currentPosition = initialPosition;
pen = Pen.PEN_UP;
this.direction = Direction.RIGHT;
}
public void turtle(String [] command)
{
int count = 0;
while(count < command.length)
{
char firstLetter = command[count].charAt(0);
int commandCode = Character.getNumericValue(firstLetter);
switch (commandCode)
{
case 1:
pen = Pen.PEN_UP;
break;
case 2:
pen = Pen.PEN_DOWN;
break;
case 3:
turn(Turn.TURN_RIGHT);
// System.out.println(direction);
break;
case 4:
turn(Turn.TURN_LEFT);
// System.out.println(direction);
break;
case 5:
String stepString = String.format ("%c%c",command[count].charAt(2),command[count].charAt(3));
int stepsCount = Integer.parseInt(stepString);
moveForward(stepsCount);
// System.out.printf("%d,%d%n",currentPosition[0],currentPosition[1]);
break;
case 6:
display();
case 9:
count = command.length;
break;
}
count++;
}
}
public void pen(int row , int column)
{
switch (pen)
{
case PEN_UP: // pen up : do nothing
break;
case PEN_DOWN: // pen down : set appropiate floor elements to 1s.
floor[row][column] = 1;
break;
}
}
public void turn(Turn turn)
{
switch (turn)
{
case TURN_RIGHT:
switch (direction)
{
case UP:
this.direction = Direction.RIGHT;
break;
case DOWN:
this.direction = Direction.LEFT;
break;
case RIGHT:
this.direction = Direction.DOWN;
break;
case LEFT:
this.direction = Direction.UP;
break;
}
case TURN_LEFT:
switch (direction)
{
case UP:
this.direction = Direction.LEFT;
break;
case DOWN:
this.direction = Direction.RIGHT;
break;
case RIGHT:
this.direction = Direction.UP;
break;
case LEFT:
this.direction = Direction.DOWN;
break;
}
}
}
public void moveForward(int spaces)
{
switch (direction)
{
case UP:
if ( currentPosition[0] - spaces >= 0 )
{
for (int i = 1; i<= spaces ; i++)
{
pen(currentPosition[0] - i , currentPosition[1]);
}
currentPosition[0] -= spaces;
}
else
{
System.out.printf("Turtle can not move further in %s direction%n",direction);
}
break;
case DOWN:
if ( currentPosition[0] + spaces < floor.length )
{
for (int i = 1; i<= spaces ; i++)
{
pen(currentPosition[0] + i , currentPosition[1]);
}
currentPosition[0] += spaces;
}
else
{
System.out.printf("Turtle can not move further in %s direction%n",direction);
}
break;
case LEFT:
if ( currentPosition[1] - spaces >= 0 )
{
for (int i = 1; i<= spaces ; i++)
{
pen(currentPosition[0] , currentPosition[1] - i);
}
currentPosition[1] -= spaces;
}
else
{
System.out.printf("Turtle can not move further in %s direction%n",direction);
}
break;
case RIGHT:
if ( currentPosition[1] + spaces < floor[0].length )
{
for (int i = 1; i<= spaces ; i++)
{
pen(currentPosition[0] , currentPosition[1] + i);
}
currentPosition[1] += spaces;
}
else
{
System.out.printf("Turtle can not move further in %s direction%n",direction);
}
}
}
public void display()
{
System.out.println();
for (int row = 0 ; row < floor.length ; row++)
{
for (int column = 0 ; column < floor[row].length ; column++)
{
if (floor[row][column] == 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
二等:
public class TurtleGraphicsTest
{
public static void main (String [] args)
{
int [] initialPosition = {3,3};
TurtleGraphics turtle = new TurtleGraphics(initialPosition);
String [] command = {"2","5,12","3","5,12","3","5,12","3","5,12","1","6","9"} ;
turtle.turtle(command);
}
}
【问题讨论】:
标签: java arrays turtle-graphics