Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何保存变量的值并防止它们在执行递归时发生更改?还有递归辅助?_Java - Fatal编程技术网

Java 如何保存变量的值并防止它们在执行递归时发生更改?还有递归辅助?

Java 如何保存变量的值并防止它们在执行递归时发生更改?还有递归辅助?,java,Java,输出: 三角形的某一支腿太长,这会使输出稍有偏差。可能是因为代码(长度/2)太长了?让我们调试一下 否则,它是完全好的,递归是伟大的,这正是我想要做的 当您不断绘制Y时,我建议您创建一种方法,在给定某些参数(例如长度、Y的两个分支之间的分离角度、旋转等)的情况下绘制Y。这将使您的代码更可读、更容易理解 至于移动到中心,只要想想坐标平面上的Y。根据Y轴的旋转及其起点,可以计算中心点 把它分解成x和y分量 根据这些信息,我们可以求解a和b import gpdraw.*; public cla

输出:

三角形的某一支腿太长,这会使输出稍有偏差。可能是因为代码(长度/2)太长了?让我们调试一下

否则,它是完全好的,递归是伟大的,这正是我想要做的
当您不断绘制Y时,我建议您创建一种方法,在给定某些参数(例如长度、Y的两个分支之间的分离角度、旋转等)的情况下绘制Y。这将使您的代码更可读、更容易理解

至于移动到中心,只要想想坐标平面上的Y。根据Y轴的旋转及其起点,可以计算中心点

把它分解成x和y分量

根据这些信息,我们可以求解ab

import gpdraw.*;

public class Y2K {

// Attributes
SketchPad pad;
DrawingTool pen;

// Constructor
public Y2K() {

    pad = new SketchPad(600, 600, 50);
    pen = new DrawingTool(pad);

    // Back the pen up so the Y is drawn in the middle of the screen
    pen.up();
    pen.setDirection(270);
    pen.forward(150);
    pen.down();
    pen.setDirection(90);
}

public void drawY(int level, double length) {

    // Base case:  Draw an Y
    if (level == 0) {

        //pen.setDirection(90);
        pen.forward(length);
        pen.turnRight(60);
        pen.forward(length);
        pen.backward(length);
        pen.turnLeft(120);
        pen.forward(length);
        pen.backward(length);
    }


    // Recursive case:  Draw an L at each midpoint
    // of the current L's segments
    else {


            //Drawing the bottom "leg" of our Y shape
            pen.forward(length / 2);
            double xpos1 = pen.getXPos();
            double ypos1 = pen.getYPos();
            double direction1 = pen.getDirection();


            pen.turnRight(90);
            drawY(level - 1, length / 2.0);

            pen.up();
            pen.move(xpos1, ypos1);
            pen.setDirection(direction1);
            pen.down();
            pen.forward(length / 2);

            double xpos2 = pen.getXPos();
            double ypos2 = pen.getYPos();
            double direction2 = pen.getDirection();

            //Drawing upper Right Leg
            pen.turnRight(60);
            pen.forward(length / 2); //going to the midpoint
            double xpos3 = pen.getXPos();
            double ypos3 = pen.getYPos();
            double direction3 = pen.getDirection();
            pen.turnLeft(90);
            drawY(level - 1, length / 2.0);

            pen.up();
            pen.move(xpos3, ypos3);
            pen.setDirection(direction3);
            pen.down();
            pen.forward(length / 2);

            //drawing upper left leg
            pen.up();
            pen.move(xpos1, ypos1);
            pen.setDirection(direction1);
            pen.down();
            pen.forward(length / 2);

            pen.turnLeft(60);
            pen.forward(length / 2);
            double xpos4 = pen.getXPos();
            double ypos4 = pen.getYPos();
            double direction4 = pen.getDirection();

            pen.turnLeft(90);
            drawY(level - 1, length / 2.0);

            pen.up();
            pen.move(xpos4, ypos4);
            pen.setDirection(direction4);
            pen.down();
            pen.forward(length / 2);
            pen.forward(length / 2);
        }

}

public static void main(String[] args) {

    Y2K fractal = new Y2K();

    // Draw Y with given level and side length
    fractal.drawY(8, 200);
}   


}
然后将其添加到x和y以计算y的中心点

至于保持恒定长度,你知道水平。在第一级,级别==1。但是下一个级别的长度应该是length*(2^level)。在本例中,长度为/2(长度为-1)

用伪代码术语:

a = length * sin(θ)
b = length * cos(θ)

您正在执行以下操作:drawYFractalExp2(级别-1,长度/2.0);。你自己把长度分成两半。如果你不除以2,它的长度将保持不变。递归涉及到将长度除以2,那么我该如何合并这个事实呢?哦,愚蠢的我,忽略前面的评论。你知道目前的水平,这是成比例的多少倍,你已经除以2。您可以传递一个恒定的长度,然后根据所处的级别除以2。只是想知道你想做什么?不,不是,我会试着融入其中?你能给我一些伪代码吗?对不起,我误解了你在做什么。我将用伪代码编辑我的答案。
public void drawY(int level, double length)
{
    //Drawing the bottom "leg" of our Y shape
    Move Forward length/2
    Save our position 
    Save our direction

    Turn to the right 90 degrees
    Recursion (call drawY())

    revert to original location
    revert to original direction
    move forward length/2 (to go to center point of Y)

    save our new position
    save our new direction 

    //Drawing upper Right Leg
    Turn 60 to the right
    Move Forward length/2 //going to the midpoint
    save our new position (don't forget the center point)
    save our new direction (don't forget the center point direction)
    Turn 90 to the left
    Recursion (call drawY())

    return to our saved position (not center one)
    return to our saved direction (not center one)

    move forward length/2

    //drawing upper left leg
    return to center point
    return to center direction

    turn left 60 
    move forward length/2
    save position (you can overwrite the center one now
    save direction (you can overwrite)

    turn left 90
    Recursion (call drawY())

    return to position
    return to direction
    move forward length/2
}