如何使用用户输入和度数

时间:2017-02-25 01:45:32

标签: 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("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.");
                }

            }

        }

    }

下一课

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;
}

这是我的代码,我坚持一件事。当您播放代码时,它会询问用户输入,结果如下:

Enter a command:
left
Number of degrees:

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

Enter a command: That is an invalid command.

我不知道我应该输入什么程度让它听。

2 个答案:

答案 0 :(得分:0)

正如我在评论中指出的那样,您应该使用next()代替nextLine()。问题是nextInt()nextDouble()不使用换行符,因此在下一个nextLine()方法调用中,剩余部分会消耗掉,显然它不会对应任何条目。< / p>

Ergo,使用next(),使用nextLine()解析整数或在读取整数后触发nextLine()

三种可能的解决方案:

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

答案 1 :(得分:0)

解决输入问题后,您的下一个障碍就是forward()方法。因为你在计算中包含了j本身,所以会发生两件坏事:1)第一步乘以0,所以乌龟实际上并没有移动; 2)乌龟加速而不是以线性方式移动。

以下是对forward()修复代码的修改以及@ Jyr关于.next().nextLine()的优秀建议,并将.toLowerCase()的结果保存回{ {1}}。我已将command折叠到Turtle对象中以简化此示例代码:

main()

<强> USAGE

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

<强>输出

enter image description here