【问题标题】:recursive levels and loop counting递归级别和循环计数
【发布时间】:2014-02-17 18:41:52
【问题描述】:

我有一个家庭作业,我准备上交,在作业中,我不得不使用递归来绘制 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

【问题讨论】:

    标签: java loops recursion counting


    【解决方案1】:

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

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

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

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

    然后,您的代码 调用 drawRCircles 函数 11 次,但自上次调用以来仅 绘制 10 个圆圈,级别 = 0 未通过测试 @ 987654325@.

    它创建的图像下降到我无法数出圆圈:/

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

    回到上面的代码,忽略draw main circle 部分(因为它独立于你的递归圆图)

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

    【讨论】:

    • 感谢您的信息!两个发布的解决方案都有效,但我会将您的解决方案标记为正确答案,因为您还对其进行了更深入的研究并给出了一些指示,谢谢:D
    猜你喜欢
    • 2020-04-11
    • 2013-03-26
    • 2015-04-23
    • 1970-01-01
    • 2021-10-20
    • 2012-11-18
    • 2023-03-24
    • 2018-06-27
    • 2017-01-21
    相关资源
    最近更新 更多