2017-02-25 6 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."); 
       } 

      } 

     } 

    } 

度まではどのようにユーザーの入力を使用して、次のクラス

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. 

が付属しています:あなたはそれがユーザーの入力を要求コードを再生すると、これは、それがどのようになるでそれを聴かせる程度をタイプするとされています。

+0

サイドノート:: 'command.toLowerCase私は、この例のコードを簡素化するためにタートルオブジェクトにmain()を折り畳まれてきました(); '単に' String'を返し、 'command'自体は変更しません。代わりに 'command = commandLowCase();'を使用してください。 – Jyr

+0

しかし、ユーザーからの入力を求めているときは、コマンドを入力すると、人は "left"と入力し、次にドットを何度も動かすかどうかを尋ねます。これは「次数」のようなもので、「90」と入力すると「これは無効なコマンドです」というメッセージが表示されます –

+0

これは 'nextLine()'を使用しているためです。 – Jyr

答えて

0

コメントに記載されているとおり、nextLine()の代わりにnext()を使用してください。問題は、nextInt()nextDouble()は改行文字を消費しないため、次のnextLine()メソッド呼び出しでは余りが消費され、明らかにそれはあなたのエントリに対応しません。

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

あなたはRESOたらあなたの入力問題を抱えているなら、次のハードルはあなたのforward()メソッドです。計算にはjが含まれているので、2つの悪いことが起こります。1)最初のステップに0が乗算され、カメが実際に動かないようにします。 2)カメは直線的に動くのではなく加速する。以下は

.next().nextLine()に関するバックcommand.toLowerCase()の結果を保存するforward()で固定だけでなく、Jyrの優れた提案を@あなたのコードの手直しです。

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

関連する問題