java - 递归级别和循环计数

标签 java loops recursion counting

我有一个作业准备交,在作业中我必须使用递归来绘制10层深的嵌套圆,在我为此敲了几个小时后我终于完成了。我唯一的问题是,我绘制的图像是 10 层深还是实际上 11 层?

这个问题来自于这样一个事实:我已经明确指出递归在经过 10 层时结束,但我确实告诉该方法绘制原始圆圈,然后它调用自身。这让我觉得它绘制了第一个级别,然后下降 10 个级别,总共 11 个级别。它创建的图像下降得如此之远,以至于我无法数出圆圈:/

如有任何澄清,我们将不胜感激,谢谢!

    // import statements
    import java.awt.*;
    import javax.swing.*;

    public class RecursiveCircles 
    {

public static void main(String[] args) 
{
    // create a new canvas
    RCanvas myCanvas = new RCanvas();

    // create JFrame
    JFrame myJframe = new JFrame();
    myJframe.setTitle("Recursive Circles");

    // set JFrame size, location and close operation
    myJframe.setSize(1500, 500);
    myJframe.setLocation(100, 100);
    myJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // and canvas to JFrame and make it visble
    myJframe.getContentPane().add(myCanvas);
    myJframe.setVisible(true);

     } // end main          
   } // end class RecursiveCircles

    /*
     * this class will draw the main circle of the image and will have the recursive
     * method that draws the two outer circles down 10 levels
     */

    class RCanvas extends Canvas 
    {
   // defualt constructor 
   public RCanvas ()
   {}

   public void paint (Graphics graphics)
   { 
       // declare variables
       String title = "Recursive Circles";

       int n = 300; // diamerter of the circle
       int xOrigin = 600; // x location of start of circle (makes circle around the origin I wanted
       int yOrigin = 100; // y location of start of circle (makes circle around the origin i wanted
       int radius = n/2; // radius of circle (half the diamerter

       int level = 10;

       // make canvas background color black
       graphics.setColor(Color.black); // make the background color black
       graphics.fillRect(0, 0, 1500, 500); // rectangle that fills the JFrame

       // put title on canvas
       graphics.setColor(Color.BLUE);
       graphics.drawString(title, 725, 50);

       // draw main circle
       graphics.setColor(Color.WHITE);
       graphics.drawOval(xOrigin, yOrigin, n, n);   

      drawRCircles(graphics,xOrigin,yOrigin,radius,level); // call recrusrive method
       System.out.println(level);

   } // end paint

   /*
    * This is the recursive method that will draw the two circles on the outer sides of the
    * main circle. it will then call itself and repate the process till it is 10 levels deep
    */
   public void drawRCircles(Graphics graphics,int xOrigin,int yOrigin, int radius, int level)
   {

   int newRadius = (radius/2); // radius of smaller circle
   int newXOrigin = xOrigin - (newRadius); // xOrigin of circle on left of the main circle
   int newYOrigin = yOrigin + (newRadius); // yOrigin of circle on the right of the main circle
   int newXOrigin2 = xOrigin + (newRadius*3); // xOrigin of circle on the right of the main circle
   int newYOrigin2 = yOrigin + (newRadius); // yOrigin of circle on the right of the main circle
       if (level > 0) // counts down from 10 to make the recursive image 10 levels deep
       { 
           graphics.drawOval(newXOrigin, newYOrigin, newRadius*2, newRadius*2); // draw recursive circle on the left of main circle
           graphics.drawOval(newXOrigin2, newYOrigin2, newRadius*2, newRadius*2); // draw recursive circle on the right of main circle
           drawRCircles(graphics, newXOrigin, newYOrigin , newRadius, (level-1)); // make recursion of left circle
           drawRCircles(graphics, newXOrigin2, newYOrigin2,newRadius,(level-1)); // make recursion of right circle

       }// end if
   } // end drawRCircles
  }// end class

最佳答案

您的代码正在绘制 11 个圆圈,但您对 drawRCircles 本身的调用仅负责其中的 10 个。

更具体地说,这些线绘制了 1 个圆圈。

    // draw main circle
    graphics.setColor(Color.WHITE);
    graphics.drawOval(xOrigin, yOrigin, n, n);  

无论您是否有drawRCircles函数,都会绘制这个圆。 (尝试删除它并运行您的代码,看看会发生什么。)

然后,您的代码调用 drawRCircles 函数 11 次,但自上次调用以来仅绘制 10 个圆圈,且 level = 0 测试失败 if(level > 0)

the Image it creates goes down so far that I can not count the circles :/

一个快速提示:因为您想知道,给定 N 的最大级别,它是否会绘制 NN+1 级别对于圆圈,您也可以尝试将 level 变量更改为更易于管理的值(例如 2)并进行直观检查。

回到上面的代码,并忽略绘制主圆部分(因为它独立于您的递归圆绘制)

    drawRCircles(graphics,xOrigin,yOrigin,radius,level); // call recursive method
    System.out.println(level);

并且在您的 public void drawRCircles(…) 函数中,

    drawRCircles(…,level-1);

让我们用 level = 2 而不是 10 来检查它:

    drawRCircles(…, 2) 
        --> Check if 1 > 0.  
            --> Yes, 1 > 0 so drawRCircles(…, 1) 
                -->  Check if 0 > 0.
                    --> No, 0 = 0, so stop.

级别数 = 2 = 绘制的递归圆数。

关于java - 递归级别和循环计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21836814/

相关文章:

math - Swift 枚举递归关联值

recursion - 方案尾递归/迭代

Guava :迭代 Multimap 的键-> 集合条目的最佳方法?

java - 非法转义字符 "\("?

java - 如何创建由 jmx 公开并通过 jconsole 访问的性能计数器?

java - 使用 Java 在普通 TCP 客户端和 Netty 服务器之间交换 Protobuf 消息

java - 什么会导致 for 循环在应该递增时递减?

python - 当平均值高于另一个列表中的相同条目时,将列表中的 None 替换为最后 3 个非 None 条目的平均值

c - 在带有 while 循环的 C 中使用递归函数的阶乘程序 c

java - 使用 javac 进行 Ant 编译未找到主类