【问题标题】:turtle in Java not working - class [closed]Java中的乌龟不工作 - 类[关闭]
【发布时间】:2014-03-01 00:59:25
【问题描述】:

我的班级结构有问题。我已经在下面发布了我的代码。我遇到的问题是确定如何在较小的函数中使用扫描仪值。我正在尝试将我的 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);
      }
    }
  }
}

【问题讨论】:

  • 您是否遇到了某种错误?
  • 是的,我在我的 Eclipse 程序中得到 X,说我需要更改 S 和 N。此外,我不确定在哪里初始化海龟函数,因为 drawsnowflake 和雪花部分。
  • 此代码无法编译。即使这样做了,在构建之后你也不会对你的海龟做任何事情。
  • 修复了你的缩进,但你似乎有一个语法错误,只有在其他两种方法之后才关闭 main 方法。也许这只是你的一个错字?
  • 看起来它们不需要是全球性的。您可以从您的 main 方法中调用 drawSnowFlake() 并为其提供用户刚刚提供的 NS 值。此外,您需要在两个方法声明中定义变量 NS 的类型。看样子,可能是int

标签: java turtle-graphics


【解决方案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);

                }
            }
        }

【讨论】:

  • 感谢@2rs2ts,但我仍然遇到与 S 和 N 相同的问题。关闭主功能后,它告诉我 S 和 N 没有被使用。
  • @user2908101 如果你像上面那样直接复制 spacitron 的代码,当然不是。你需要给乌龟一些指示。我建议调用你的 Snowflake 类的方法,看看会发生什么。
  • @user2908101 我想进一步帮助你,但如果你不知道如何调用方法,也许你需要先学习一些初学者教程,然后才能期待其他人解决你的问题。
  • 谢谢你们。我想通了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 2012-11-07
  • 2021-12-17
  • 2017-08-09
  • 2018-03-29
  • 1970-01-01
  • 2021-09-26
相关资源
最近更新 更多