Java中的乌龟不起作用 - 班级

时间:2014-02-04 20:42:50

标签: java turtle-graphics

我的班级结构有困难。我在下面发布了我的代码。我遇到的问题是确定如何在较小的功能中使用扫描仪值。我正在尝试将我的N和S值用于我的两个雪花函数。

import java.util.Scanner;

public class Snowflake {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter S: ");
    int S = sc.nextInt();
    System.out.println("Enter N: ");
    int N = sc.nextInt();

    Turtle turtle  = new Turtle(0, 0, 60);

    public void snowflakepart(S, N) {
      int z = 1;

      if (N > 0) {
        turtle.goForward(S);

        if (N > 1) {
          turtle.turnLeft(120.0);

          while (z <= 5) {
            snowflakepart(S/3, N-1);
            turtle.turnRight(60.0);
            turtle.turnRight(180.0);
            turtle.turnLeft(180.0);
            turtle.goForward(S);
            turtle.turnRight(180.0);    
          }
        }
      }
    }

    public void drawSnowflake(S,N) {    
      int y = 1;

      while (y <= 6) {
        snowflakepart(S,N);
        turtle.turnLeft(60.0);
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我会冒险猜测,因为你没有提供太多信息。我看到你的代码最大的问题是你把错误的东西放错了地方。也许你想做这样的事情:

public class Snowflake {
        Turtle turtle;
     //Create a Turtle object here since you'll use it in this class' methods.
        public Snowflake(){
            turtle  = new Turtle(0, 0, 60);
        }
        public static void main(String[] args) {
          //Get a reference to your SnowFlake object here and then use this part for
          //user interaction only
            SnowFlake flake = new Snowflake();
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter S: ");
            int S = sc.nextInt();
            System.out.println("Enter N: ");
            int N = sc.nextInt();

        //Call your functions here with the parameter your users just entered

        }

          //These 2 methods belong to the SnowFlake class and so they need to be 
          //declared in its body
            public void snowflakepart(int S, int N) {
                int z = 1;
                if(N > 0) {
                    turtle.goForward(S);
                    if(N>1){
                        turtle.turnLeft(120.0);
                        while(z<=5){
                            snowflakepart(S/3, N-1);
                            turtle.turnRight(60.0);
                        turtle.turnRight(180.0);
                    turtle.turnLeft(180.0);
                    turtle.goForward(S);
                    turtle.turnRight(180.0);    
                    }
                }
                }
            }
            public void drawSnowflake(int S, int N) {    
                int y = 1;
                while(y <= 6){
                    snowflakepart(S,N);
                    turtle.turnLeft(60.0);

                }
            }
        }