【问题标题】:how to convert code into recursion?如何将代码转换为递归?
【发布时间】:2015-02-13 23:51:53
【问题描述】:
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class DrawIt
{

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        final int width = 400;
        final int height = 400;
        frame.setSize(width,height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent component = new JComponent()


        {
            public void paintComponent(Graphics graph){
                draw(graph);
            }
        };
        frame.add(component);
        frame.setVisible(true);

    }

    public static void draw(Graphics g)
    {
        int  x1=100;
        int y1 = 100;
        int length = 10;

        for(int i=0;i<=10;i++)
        {
            int x2 = x1 + length;
            int y2 = y1;
            g.drawLine(x1,y1,x2,y2);

            x1=x2;
            y1=y2;

            y2=y1-length;
            g.drawLine(x1,y1,x2,y2);
            x1=x2;
            y1=y2;
            length+=10;

            x2=x1-length;
            g.drawLine(x1, y1, x2, y2);
            x1=x2;
            y1=y2;
            y2=y1+length;
            g.drawLine(x1, y1, x2, y2);
            x1=x2;
            y1=y2;
            length+=10;
        }
    }
}

如何将其转换为递归?这给了我矩形螺旋但不是递归的方式。帮助。如果这样我使用4个变量,递归,会怎么样?

【问题讨论】:

  • 这将是一个以这些变量作为参数的方法。您可以通过递归和传递其他内容来更改它们。

标签: java recursion coding-style drawrectangle spiral


【解决方案1】:
public class DrawIt {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        final int width = 400;
        final int height = 400;
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent component = new JComponent() {
            public void paintComponent(Graphics graph) {
                draw(graph);
            }
        };
        frame.add(component);
        frame.setVisible(true);

    }

    public static void drawpuzzle(Graphics g, int x1, int y1, int length, int count) {
        if (count> 0) {
            int x2 = x1 + length;
            int y2 = y1;
            g.drawLine(x1, y1, x2, y2);

            x1 = x2;
            y1 = y2;

            y2 = y1 - length;
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
            length += 10;

            x2 = x1 - length;
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
            y2 = y1 + length;
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
            length += 10;
            drawpuzzle(g, x1, y1, length, count - 1);
        }

    }

    public static void draw(Graphics g) {
        int x1 = 100;
        int y1 = 100;
        int length = 10;
        int count = 10;
        drawpuzzle(g, x1, y1, length, count);

    }
}

【讨论】:

    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2021-07-27
    • 2013-11-02
    • 1970-01-01
    相关资源
    最近更新 更多