2017-02-23 97 views
0

我正在研究一個項目,我們應該有一個可以將指令向前,向左,向右,penUp,penDown,penColor並退出的烏龜。當筆落下時,它應該在點之間繪製一條線。當烏龜移動它應該留下足跡。左和右應該改變方向的度數。到目前爲止,我無法得出更多的觀點。這是我的代碼至今一類主要和一個烏龜海龜項目Java

import java.util.Scanner; 
public class Main { 

public static void main(String[] args) { 
    Turtle t = new Turtle(); 

    Scanner keyboard = new Scanner(System.in); 
    String command; 

    StdDraw.setPenRadius(0.05); 
    t.Draw(); 


    do { 

    System.out.println("Enter A Command: forward, right, left, penup, pendown,  pencolor, or quit"); 
    command = keyboard.next(); 


    if (command.equalsIgnoreCase("quit")){ 

     break; 
    } 
     else if (command.equalsIgnoreCase("pencolor")) { 

     System.out.println("Enter A Color: Red, Green, Black, Yellow, Blue"); 
     String color = keyboard.next(); 
     t.setColor(color); 
     t.Draw(); 
    } 

    else if (command.equalsIgnoreCase("forward")) { 
     System.out.println("Enter The Number of Steps You Would Like To Move"); 
     int steps = keyboard.nextInt(); 
     t.moveForward(steps); 
     t.Draw(); 

    } else if (command.equalsIgnoreCase("right")) { 
     System.out.println("Enter The Number Of Degrees You Would Like To Move"); 
     String radAngle = keyboard.next(); 
     t.Draw(); 

    } else if (command.equalsIgnoreCase("left")) { 
     System.out.println("Enter The Number Of Degrees You Would Like To Move"); 
     String radAngle = keyboard.next(); 
     t.Draw(); 

    } else if (command.equalsIgnoreCase("penup")) { 
     t.putPenUp(); 
     t.Draw(); 

    } else if (command.equalsIgnoreCase("pendown")) { 
     t.putPenDown(); 
     t.Draw(); 

    }} while (!(command.equalsIgnoreCase("quit"))); 

} 
} 



public class Turtle { 
private double location; 
public double xCoord; 
public double yCoord; 
double direction; 
private boolean penPosition; 
public static boolean penDown = false; 
public static boolean penUp = true; 
private int red; 
private int green; 
private int blue; 
private int steps; 

public Turtle() { 
    xCoord = .5; 
    yCoord = .5; 
    penPosition = penUp; 
    location = 90; 
} 

public void Draw(){ 

    StdDraw.setPenColor(StdDraw.BLACK); 
    StdDraw.point(xCoord, yCoord); 

} 

public void setColor(String color) { 
if (color.equalsIgnoreCase("red")) { 
    red = 255; 
    green = 0; 
    blue = 0; 
} else if (color.equalsIgnoreCase("green")) { 
    red = 0; 
    green = 255; 
    blue = 0; 
} else if(color.equalsIgnoreCase("blue")) { 
    red = 0; 
    green = 0; 
    blue = 255; 
} else if(color.equalsIgnoreCase("yellow")) { 
    red = 255; 
    green = 255; 
    blue = 0; 
} else if(color.equalsIgnoreCase("black")) { 
    red = 0; 
    green = 0; 
    blue = 0; 
} else { 
     red = 0; 
     green = 0; 
     blue = 0; 
    } 
} 


public void moveForward(int steps) { 

    double radAngle = Math.toRadians(location); 


    double newx = xCoord + (Math.cos(radAngle) * steps); 
    double newy = yCoord + (Math.sin(radAngle) * steps); 
    StdDraw.point(newx, newy); 
    StdDraw.line(xCoord, yCoord, newx, newy); 
    StdDraw.show(); 
    } 



    public void putPenDown() { 
    penPosition = penDown; 
    if (true) { 
     // StdDraw.line(x, y, xCoord, yCoord); 
    } 
} public void putPenUp() { 
    penPosition = penUp; 
    // StdDraw.line(xCoord, yCoord, newx, newy); 
} 
} 

回答

0

你的程序有幾個問題:StdDraw認爲座標平面的爲0.0將1.0讓你的整數steps沒有意義,除非你將其縮放到StdDraw座標中 - 在我的示例中,我將該平面劃分爲100 steps;您的moveForward()函數在移動後沒有將xCoordyCoord變量設置爲新位置,因此龜沒有真正去過任何地方;目前還不清楚爲什麼你依靠你的Draw()這麼多,因爲它並沒有太多的工作;程序退出邏輯被破壞;轉向邏輯被打破並且不完整。

在下面的返工中,我作了一些簡化以用於示例目的:我已將main()例程移動到Turtle以避免額外的文件/類;我已經實現了更簡單的顏色,因爲你有什麼不工作 - 你可以加回你的RGB模型;我簡化了筆的上/下邏輯,使其更實用。

import java.awt.Color; 
import java.util.Scanner; 

public class Turtle { 
    private double degAngle; 
    public double xCoord; 
    public double yCoord; 
    private boolean penDown; 
    private Color penColor; 
    private int steps; 

    public Turtle() { 
     xCoord = 0.5; 
     yCoord = 0.5; 
     penDown = true; 
     penColor = StdDraw.BLACK; 
     degAngle = 90; 
     StdDraw.setPenRadius(0.01); 
    } 

    public void Draw() { 
     if (penDown) { 
      StdDraw.setPenColor(penColor); 
      StdDraw.point(xCoord, yCoord); 
     } 
    } 

    public void setColor(String color) { 
     if (color.equalsIgnoreCase("red")) { 
      penColor = StdDraw.RED; 
     } else if (color.equalsIgnoreCase("green")) { 
      penColor = StdDraw.GREEN; 
     } else if (color.equalsIgnoreCase("blue")) { 
      penColor = StdDraw.BLUE; 
     } else if (color.equalsIgnoreCase("yellow")) { 
      penColor = StdDraw.YELLOW; 
     } else { 
      penColor = StdDraw.BLACK; 
     } 

     this.Draw(); // show new color 
    } 

    public void moveForward(int steps) { 

     double radAngle = Math.toRadians(degAngle); 
     double newx = xCoord + (Math.cos(radAngle) * steps/100); 
     double newy = yCoord + (Math.sin(radAngle) * steps/100); 

     if (penDown) { 
      StdDraw.setPenColor(penColor); 
      StdDraw.line(xCoord, yCoord, newx, newy); 
     } 

     xCoord = newx; 
     yCoord = newy; 
    } 

    public void turnRight(double angle) { 
     degAngle += -angle; 
    } 

    public void turnLeft(double angle) { 
     degAngle += angle; 
    } 

    public void putPenDown() { 
     penDown = true; 
    } 

    public void putPenUp() { 
     penDown = false; 
    } 

    public static void main(String[] args) { 

     Turtle t = new Turtle(); 

     Scanner keyboard = new Scanner(System.in); 
     String command; 

     t.Draw(); // show turtle 

     do { 
      System.out.println("Enter a command: forward, right, left, penup, pendown, pencolor, or quit"); 

      command = keyboard.next(); 

      if (command.equalsIgnoreCase("quit")) { 
       break; 

      } else if (command.equalsIgnoreCase("pencolor")) { 
       System.out.println("Enter a color: red, green, black, yellow, blue"); 
       t.setColor(keyboard.next()); 

      } else if (command.equalsIgnoreCase("forward")) { 
       System.out.println("Enter the number of steps you would like to move"); 
       t.moveForward(keyboard.nextInt()); 

      } else if (command.equalsIgnoreCase("right")) { 
       System.out.println("Enter the number of degrees you would like to turn"); 
       t.turnRight(keyboard.nextDouble()); 

      } else if (command.equalsIgnoreCase("left")) { 
       System.out.println("Enter the number of degrees you would like to turn"); 
       t.turnLeft(keyboard.nextDouble()); 

      } else if (command.equalsIgnoreCase("penup")) { 
       t.putPenUp(); 

      } else if (command.equalsIgnoreCase("pendown")) { 
       t.putPenDown(); 
      } 

     } while (true); 

    System.exit(0); 
    } 
} 

用法

% java Turtle 
Enter a command: forward, right, left, penup, pendown, pencolor, or quit 
pencolor 
Enter a color: red, green, black, yellow, blue 
green 
Enter a command: forward, right, left, penup, pendown, pencolor, or quit 
forward 
Enter the number of steps you would like to move 
20 
Enter a command: forward, right, left, penup, pendown, pencolor, or quit 
left 
Enter the number of degrees you would like to turn 
120 
Enter a command: forward, right, left, penup, pendown, pencolor, or quit 
forward 
Enter the number of steps you would like to move 
20 
Enter a command: forward, right, left, penup, pendown, pencolor, or quit 
left 
Enter the number of degrees you would like to turn 
120 
Enter a command: forward, right, left, penup, pendown, pencolor, or quit 
forward 
Enter the number of steps you would like to move 
20 
Enter a command: forward, right, left, penup, pendown, pencolor, or quit 
quit 
% 

輸出

enter image description here

+0

謝謝你這麼多的人 –