递归章节后面的程序设计项目题.老外的题目就是怪
想了半天做不出..谁能帮忙做下.编写一个能产生如下所示输出结果的方法.所示的输出结果
是通过调用数值1产生的.在这个例子中,递归到达4层深度时停止,
但要求所编写的方法应该能继续到任意指定层(注意空格). This was written by call number 2.
  This was written by call number 3.
   This was written by call number 4.
   This ALSO written by call number 4.
  This ALSO written by call number 3.
 This ALSO written by call number 2.
This ALSO written by call number 1.

解决方案 »

  1.   


    public static void itePrint(int i,int stopLevel)
    {
    if(i==stopLevel)
    {
    System.out.println("also "+ i);
    }
    else
    {
    System.out.println("was "+ i);
    itePrint(i+1,stopLevel);
    System.out.println("also "+ i);
    }
             }        public static void main(String[] args) throws UnsupportedEncodingException 
    {
    itePrint(1,4);
            }
      

  2.   

    public class Main {
        public static void main(String[] args) {
            print(2, 4, 1);
        }    void print(int min, int max, int index) {
            if (index >= min) {
                if (index > min) {
                    System.out.print("  ");
                }
                System.out.println("This was written by call number " + index + ".");
            }
            if (index < max) {
                print(base, length, index + 1);
            }
            if (index > min) {
                System.out.println("  ");
            }
            System.out.println("This ALSO written by call number " + index + ".");
        }
    }