Question

I'm having difficulty with my class structure. I've posted my code below. The problem I'm having is determining how to use scanner values in smaller functions. I'm trying to use my N and S values for my two snowflake functions.

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);
      }
    }
  }
}
Était-ce utile?

La solution

I'll hazard a guess since you're not providing much information. The biggest problem I see with your code is that you're putting the wrong thing in the wrong place. Maybe you want to do something like this:

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

                }
            }
        }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top