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.
Randbemerkung: 'command.toLowerCase(); 'gibt nur einen' String' zurück und ändert 'command' selbst nicht. Verwenden Sie stattdessen 'command = command.toLowerCase();'. – Jyr
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" –
Das ist, weil Sie 'nextLine()' verwenden, stattdessen 'next()' verwenden. – Jyr