Java 递归级别和循环计数

Java 递归级别和循环计数,java,loops,recursion,counting,Java,Loops,Recursion,Counting,我有一个作业要交,作业中我不得不用递归法画10层深的嵌套圆圈,在我的头撞了几个小时后,我终于完成了。我唯一的问题是,我画的图像是10层深还是11层深 这个问题来自于这样一个事实,我已经特别声明了递归在经过10个级别后结束,但是我确实告诉了这个方法绘制原始的圆,然后它调用自己。这让我觉得它画出了第一个关卡,然后下降了10个关卡,总共有11个关卡。它创建的图像下降到我无法计数的地步:/ 如有任何澄清,将不胜感激,谢谢 // import statements import java.

我有一个作业要交,作业中我不得不用递归法画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个圆

更具体地说,这些线绘制了一个圆

    // draw main circle
    graphics.setColor(Color.WHITE);
    graphics.drawOval(xOrigin, yOrigin, n, n);  
无论您是否有
drawricrcles
功能,都会绘制此圆。(尝试删除它并运行代码以查看发生了什么。)

然后,您的代码调用
drawRCircles
函数11次,但自上次调用该函数以来仅绘制了10个圆,如果(级别>0),级别=0,则测试将失败
if(级别>0)

它创建的图像下降得如此之深,以至于我无法计算圆数:/

一个快速提示:由于您想知道,如果给定
N
的最大级别,它是否会绘制
N
N+1
级别的圆圈,您也可以尝试将
level
变量更改为更易于管理的变量(如2),并目视检查它

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

    drawRCircles(graphics,xOrigin,yOrigin,radius,level); // call recursive method
    System.out.println(level);
并且在您的
公共作废绘图循环(…)
函数中

    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=绘制的递归圈数。

感谢您提供的信息!这两个贴出的解决方案都有效,但我将把你的答案标记为正确答案,因为你还深入研究了一下,并给出了一些建议,谢谢:D