gpt4 book ai didi

java - Java 中的 turtle 不工作 - 类

转载 作者:行者123 更新时间:2023-12-01 13:31:11 24 4
gpt4 key购买 nike

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

最佳答案

由于您没有提供太多信息,我会冒险猜测。我发现您的代码最大的问题是您将错误的内容放在错误的位置。也许你想做这样的事情:

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

}
}
}

关于java - Java 中的 turtle 不工作 - 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21562972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com