【问题标题】:Turtle Graphics direction not working (java)海龟图形方向不起作用(java)
【发布时间】: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


    【解决方案1】:

    您在turn(Turn)case 语句中错过了break。所以右转后,你也进入case TURN_LEFT,你就有了原来的方向。

    你的方法应该是这样的

    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;
                }
                break; // necessary
            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;
                }
                break; // optional
        }
    }
    

    【讨论】:

    • 谢谢你,先生,我不知道我是怎么错过的。你帮了大忙。它现在正在工作。
    • 另一个问题:如果我想将方向传递给 TurtleGraphics 类的对象,我会在 TurtleGraphicsTest 类中做什么?我试图传递一个方向,如:RIGHT 或 Direction.RIGHT 但它不起作用,因为枚举方向仅在 TurtleGraphics 类中声明。
    • 你只需要增加枚举的可见性 - 删除private(抱歉延迟回答)
    猜你喜欢
    • 2018-05-30
    • 1970-01-01
    • 2017-10-01
    • 2011-12-12
    • 2016-03-07
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多