Java 如何使用用户输入和度

Java 如何使用用户输入和度,java,turtle-graphics,Java,Turtle Graphics,下节课 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("En

下节课

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.");
                }

            }

        }

    }
但是当我输入任何一个数字时,它只是附带的

Enter a command:
left
Number of degrees:

我不知道我应该输入什么才能让学位听进去。

正如我在评论中指出的,你应该使用
next()
而不是
nextLine()
。问题是
nextLine()
nextDouble()
不使用换行符,因此在下一个
nextLine()
方法调用中,剩余部分被使用,显然它不会与任何条目对应

因此,可以使用
next()
,使用
nextLine()
解析整数,或者在读取整数后激发
nextLine()

因此,有三种可能的解决方案:

1:

String命令=scan.next()

2:

3:


一旦解决了输入问题,下一个障碍就是
forward()
方法。因为在计算中包含了
j
本身,所以会发生两件不好的事情:1)第一步乘以0,所以海龟实际上不会移动;2) 海龟加速而不是直线运动

下面是对您的代码进行的返工,修复了
forward()
以及@Jyr关于
.next()
.nextLine()
的出色建议,并将
.toLowerCase()
的结果保存回
命令中。我已将
main()
折叠到Turtle对象中,以简化此示例代码:

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);
}
输出


侧注:
command.toLowerCase()
只返回一个
字符串
,不改变
命令本身。而是使用
command=command.toLowerCase()。不,但当它要求用户输入时,它会说输入命令:然后用户会键入“left”,然后它会要求您将圆点向左移动多少度。像这样的“度数:”如果他们输入说“90”,就会出现“这是一个无效的命令”,这是因为你使用了
nextLine()
,而不是
next()
。可能是重复的
Enter a command: That is an invalid command.
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);
}
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);
}
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);
    }
}
> 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
>