Java 我如何递归地创建金字塔数模式?

Java 我如何递归地创建金字塔数模式?,java,Java,我必须使用递归方法创建这个模式 我让它打印出的数字不正确 大体上我这样称呼它 int n=5; a、 模式(n,n) 结尾应该是这样的,但我有这样的 1 1 212 211 32123 32121 4321321 等等。。。 谢谢你 这个金字塔 *********1*********

我必须使用递归方法创建这个模式 我让它打印出的数字不正确 大体上我这样称呼它 int n=5; a、 模式(n,n)

结尾应该是这样的,但我有这样的

    1                          1
   212                        211
  32123                      32121
                            4321321
等等。。。 谢谢你

这个金字塔

*********1*********
********212********
*******32123*******
******4321234******
*****543212345*****
****65432123456****
***7654321234567***
**876543212345678**
*98765432123456789*
…是这种递归方法的结果,没有用于空格和数字的方法:

public class RecursivePyramid {

    public static void WriteOutPyramidLevel(int level,int height) {

        // Need counter in loops 
        int counter;

        // Writing spaces in front - using asterix for visible debugging
        for (counter=height ;counter > level; counter-- ) 
          System.out.print("*");

        // Writing numbers in front
        for (counter=level ;counter > 1; counter-- ) 
            System.out.print(counter);                    

        // Writing central «line» in Pyramid    
        System.out.print(1);

        // Writing numbers after central line
        for (counter=2 ;counter <= level; counter++ ) 
            System.out.print(counter);

        // Writing spaces after central line - can of course be removed   
        for (counter=level ;counter < height; counter++) 
            System.out.print("*"); 

        // Line finished
        System.out.println();

        // If below height of Pyramid go again - max 9 allowed 
        if (level < height && level < 9)
          WriteOutPyramidLevel(level+1,height);        
    }

    public static void main(String[] parameters) {

        WriteOutPyramidLevel(1,10);        
    }
}

公共类递归金字塔{
公共静态void WriteOutPyramidLevel(整数级,整数高度){
//需要循环计数器吗
整数计数器;
//在前面写空格-使用asterix进行可见调试
用于(计数器=高度;计数器>水平;计数器--)
系统输出打印(“*”);
//在前面写数字
对于(计数器=级别;计数器>1;计数器--)
系统输出打印(计数器);
//在金字塔中书写中心«线»
系统输出打印(1);
//在中心线后写数字

对于(counter=2;counter)请提供创建所示输出的程序的详细信息,即使它不是您最终想要的。是什么让您认为递归是实现所需输出的正确工具?如果是“因为老师想要”,然后请完整地、逐字地和文本地引用整个作业。并应用此处描述的折衷方法。在这种情况下,请@Yunnosch我将我的代码显示在底部,我显示了它应该是什么样子。我看到了您的代码输出。我希望您将您显示的代码片段转换为一个演示如何创建t输出。为此,至少缺少对
pattern()
的根调用。您得到了一个混合循环和递归的答案。您可能需要澄清代码的超级递归概念是必需的还是允许在几个循环中混合。显示完整的赋值描述可能有助于判断这一点。
public class RecursivePyramid {

    public static void WriteOutPyramidLevel(int level,int height) {

        // Need counter in loops 
        int counter;

        // Writing spaces in front - using asterix for visible debugging
        for (counter=height ;counter > level; counter-- ) 
          System.out.print("*");

        // Writing numbers in front
        for (counter=level ;counter > 1; counter-- ) 
            System.out.print(counter);                    

        // Writing central «line» in Pyramid    
        System.out.print(1);

        // Writing numbers after central line
        for (counter=2 ;counter <= level; counter++ ) 
            System.out.print(counter);

        // Writing spaces after central line - can of course be removed   
        for (counter=level ;counter < height; counter++) 
            System.out.print("*"); 

        // Line finished
        System.out.println();

        // If below height of Pyramid go again - max 9 allowed 
        if (level < height && level < 9)
          WriteOutPyramidLevel(level+1,height);        
    }

    public static void main(String[] parameters) {

        WriteOutPyramidLevel(1,10);        
    }
}