2017-02-25 11 views
0
import java.util.Scanner; 

public class Main { 

public static void main(String[] args) { 


      Scanner scan = new Scanner(System.in); 

      Turtle t = new Turtle(); 

      while (true){ 

       System.out.println("Enter a command:"); 
       String command = scan.nextLine(); 
       command.toLowerCase(); 

       //moves the turtle forward 
       if (command.equals("forward")) 
       { 
        //further prompts the user for the number of steps 
        System.out.print("Number of steps: "); 
        int i = scan.nextInt(); 

        t.forward(i); 
       } 
       else if (command.equals("right")){ 

        System.out.print("Number of degrees: "); 
        double d = scan.nextDouble(); 

        t.right(d); 
       } 
       else if (command.equals("left")){ 
        System.out.print("Number of degrees: "); 
        double d = scan.nextDouble(); 

        t.left(d); 
       } 
       //else if (command.equals("setpencolor")){ 

        //System.out.print("New color: "); 
        //String c = scan.nextLine(); 

        //t.setPenColor(c); 
      // } 
       else if (command.equals("quit")){ 
        break; 
       } 
       else{ 
        System.out.println("That is an invalid command."); 
       } 

      } 

     } 

    } 

NÄCHSTE KLASSEWie Benutzereingabe zu verwenden und Grad

public class Turtle { 


public final int RADIUS = 5; 

private double xCoord; 
private double yCoord; 
private double direction; 
private boolean penDown; 

public Turtle(){ 
    int canvasSize = 400; 

    StdDraw.setCanvasSize(canvasSize, canvasSize); 
    StdDraw.setXscale(0, canvasSize); 
    StdDraw.setYscale(0, canvasSize); 


    xCoord = canvasSize/2; 
    yCoord = canvasSize/2; 
    direction = 90; 
    StdDraw.setPenColor(StdDraw.BLACK); 
    penDown = false; 

    StdDraw.filledCircle(xCoord, yCoord, RADIUS); 
} 

//converts degrees to radians 
public double convertToRadians(double degree){ 
    return (degree*Math.PI)/180; 
} 

public void forward(int i){ 

    double stepSize = 20; 

    //draws a turtle for each step 
    for (int j = 0; j < i; j++) 
    { 

     //draws a line connecting the turtles if penDown is true 
     if (penDown==true) 
      StdDraw.line(xCoord, yCoord, (j*stepSize*Math.cos(convertToRadians(direction))+xCoord), (j*stepSize*Math.sin(convertToRadians(direction))+yCoord)); 

     xCoord = j*stepSize*Math.cos(convertToRadians(direction)+xCoord); 
     yCoord = j*stepSize*Math.sin(convertToRadians(direction)+yCoord); 
     StdDraw.filledCircle(xCoord, yCoord, RADIUS); 
    } 

} 

//turns the turtle a degrees to the right 
public void right(double a){ 
    direction -= a; 
} 

//turns the turtle a degrees to the left 
public void left(double a){ 
    direction += a; 
} 

//makes it so a line will not be drawn between turtles 
public void penUp(){ 
    penDown = false; 
} 

//makes it so a line will be drawn between turtles 
public void penDown(){ 
    penDown = true; 
} 

Dies ist mein Code, den ich habe, und ich bin auf eine Sache stecken. Wenn Sie den Code spielen sie für die Benutzereingabe verlangt, ist dies, wie es geht:

Enter a command: 
left 
Number of degrees: 

Aber wenn ich in einer beliebigen Anzahl eingeben es kommt nur mit

Enter a command: That is an invalid command. 

Ich weiß nicht, was ich bin sollte für den Grad tippen, um es zu hören.

+0

Randbemerkung: 'command.toLowerCase(); 'gibt nur einen' String' zurück und ändert 'command' selbst nicht. Verwenden Sie stattdessen 'command = command.toLowerCase();'. – Jyr

+0

nein, aber wenn es nach der Benutzereingabe fragt, sagt es einen Befehl eingeben: dann wird die Person "links" eingeben und dann fragt sie nach ho vielen Graden, die Sie den Punkt nach links bewegen möchten.wie folgt: "Anzahl der Grade:" und wenn sie "90" eingeben, erscheint "dies ist ein ungültiger Befehl" –

+0

Das ist, weil Sie 'nextLine()' verwenden, stattdessen 'next()' verwenden. – Jyr

Antwort

0

Wie ich in den Kommentaren notiert habe, sollten Sie next() anstelle von nextLine() verwenden. Das Problem ist, dass nextInt() und nextDouble() nicht das Newline-Zeichen verbrauchen, so auf der nächsten nextLine() Methode der Rest nennen wird verbraucht und natürlich wird es nicht zu einem Ihrer Einträge entsprechen.

Ergo, verwenden Sie entweder next(), analysieren Sie die Ganzzahl mit nextLine() oder Feuer nextLine() nach dem Lesen der Ganzzahl.

So drei mögliche Lösungen:

1:

String command = scan.next();

2:

if (command.equals("forward")) 
{ 
    //further prompts the user for the number of steps 
    System.out.print("Number of steps: "); 
    int i = scan.nextInt(); 
    scan.nextLine(); 

    t.forward(i); 
} 

3:

if (command.equals("forward")) 
{ 
    //further prompts the user for the number of steps 
    System.out.print("Number of steps: "); 
    int i = Integer.parseInt(scan.nextLine()); 

    t.forward(i); 
} 
0

Sobald Sie reso Wenn Sie Ihr Eingabeproblem haben, ist Ihre nächste Hürde Ihre forward() Methode. Da Sie j selbst in die Berechnung einbezogen haben, passieren zwei schlimme Dinge: 1) Der erste Schritt wird mit 0 multipliziert, so dass sich die Schildkröte nicht wirklich bewegt; 2) die Schildkröte beschleunigt, anstatt sich linear zu bewegen.

Unten ist eine Nacharbeit des Codes mit forward() auch fixiert, wie @ ausgezeichnete Vorschläge des JYR bezüglich .next() vs. .nextLine() und das Ergebnis .toLowerCase() zurück in command speichern. Ich habe main() in das Turtle-Objekt gefaltet dieses Beispiel-Code zu vereinfachen:

import java.util.Scanner; 

public class Turtle { 

    public final int RADIUS = 5; 
    public final double STEP_SIZE = 20; 
    public final int CANVAS_SIZE = 400; 

    private double xCoord; 
    private double yCoord; 
    private double direction; 
    private boolean penDown; 

    public Turtle() { 

     StdDraw.setCanvasSize(CANVAS_SIZE, CANVAS_SIZE); 
     StdDraw.setXscale(0, CANVAS_SIZE); 
     StdDraw.setYscale(0, CANVAS_SIZE); 

     xCoord = CANVAS_SIZE/2; 
     yCoord = CANVAS_SIZE/2; 
     direction = 90; 
     StdDraw.setPenColor(StdDraw.BLACK); 
     penDown = false; 

     StdDraw.filledCircle(xCoord, yCoord, RADIUS); 
    } 

    // converts degrees to radians 
    public static double convertToRadians(double degree) { 
     return (degree * Math.PI)/180; 
    } 

    public void forward(int i) { 

     double directionInRadians = convertToRadians(direction); 

     // draws a turtle for each step 
     for (int j = 0; j < i; j++) { 

      double new_xCoord = STEP_SIZE * Math.cos(directionInRadians) + xCoord; 
      double new_yCoord = STEP_SIZE * Math.sin(directionInRadians) + yCoord; 

      // draws a line connecting the turtles if penDown is true 
      if (penDown) { 
       StdDraw.line(xCoord, yCoord, new_xCoord, new_yCoord); 
      } 

      xCoord = new_xCoord; 
      yCoord = new_yCoord; 

      StdDraw.filledCircle(xCoord, yCoord, RADIUS); 
     } 
    } 

    // turns the turtle a degrees to the right 
    public void right(double angle) { 
     direction -= angle; 
    } 

    // turns the turtle a degrees to the left 
    public void left(double angle) { 
     direction += angle; 
    } 

    // makes it so a line will not be drawn between turtles 
    public void penUp() { 
     penDown = false; 
    } 

    // makes it so a line will be drawn between turtles 
    public void penDown() { 
     penDown = true; 
    } 

    public static void main(String[] args) { 

     Scanner scan = new Scanner(System.in); 

     Turtle t = new Turtle(); 

     while (true) { 

      System.out.print("Enter a command: "); 
      String command = scan.next().toLowerCase(); 

      // moves the turtle forward 
      if (command.equals("forward")) { 
       // further prompts the user for the number of steps 
       System.out.print("Number of steps: "); 
       t.forward(scan.nextInt()); 

      } else if (command.equals("right")) { 
       System.out.print("Number of degrees: "); 
       t.right(scan.nextDouble()); 

      } else if (command.equals("up")) { 
       t.penUp(); 

      } else if (command.equals("down")) { 
       t.penDown(); 

      } else if (command.equals("left")) { 
       System.out.print("Number of degrees: "); 
       t.left(scan.nextDouble()); 

      } else if (command.equals("quit")){ 
       break; 

      } else { 
       System.out.println("That is an invalid command."); 
      } 
     } 

     System.exit(0); 
    } 
} 

USAGE

> java Turtle 
Enter a command: forward 
Number of steps: 5 
Enter a command: right 
Number of degrees: 120 
Enter a command: forward 
Number of steps: 5 
Enter a command: right 
Number of degrees: 120 
Enter a command: forward 
Number of steps: 5 
Enter a command: quit 
> 

OUTPUT

enter image description here

Verwandte Themen