Java 如何将嵌套for循环转换为递归

Java 如何将嵌套for循环转换为递归,java,loops,for-loop,recursion,Java,Loops,For Loop,Recursion,有人能帮我把这个for循环转换成递归方法吗 到目前为止,我添加了这两种方法,但我仍然希望更改第二个循环。 先谢谢你 public void makeDesign1() { int x; for (int i = 0; i < 5; i++) // For loop is the one creating the rows { for (x = 4; x > i; x--) // Nested loop is the one cre

有人能帮我把这个for循环转换成递归方法吗 到目前为止,我添加了这两种方法,但我仍然希望更改第二个循环。 先谢谢你

       public void makeDesign1() {
    int x;
    for (int i = 0; i < 5; i++) // For loop is the one creating the rows
    {
        for (x = 4; x > i; x--) // Nested loop is the one creating the columns 
        {
            System.out.print("*");
        }
        System.out.println();
    }
    System.out.println();

}

public static int makeDesign1Recur(int i) {

    if (i == 0) {
        return 0;
    }
    System.out.print("*");
    return (makeDesign1Recur(i-1));
}
// How to convert this second loop recursive?
public static void makeDesignRow(int i){
   for ( int x = i; x>=0; x--){
       makeDesign1Recur(x);
       System.out.println("");
   }


}
public void makeDesign1(){
int x;
for(int i=0;i<5;i++)//for循环是创建行的循环
{
对于(x=4;x>i;x--)//嵌套循环是创建列的循环
{
系统输出打印(“*”);
}
System.out.println();
}
System.out.println();
}
公共静态int makeDesign1Recur(int i){
如果(i==0){
返回0;
}
系统输出打印(“*”);
返回(makeDesign1Recur(i-1));
}
//如何将第二个循环转换为递归?
公共静态void makeDesignRow(int i){
对于(int x=i;x>=0;x--){
makeDesign1Recur(x);
System.out.println(“”);
}
}

我认为第一步是正确地重新定义
makeDesign1()。我们想为我们的画输入一个尺寸。我们还希望稍微更改边界,以便1的大小绘制一颗星,而不是像原始大小那样没有:

public static void makeDesign(int n) 
{
    for (int i = 0; i < n; i++) // For loop is the one creating the rows
    {
        for (int x = n; x > i; x--) // Nested loop is the one creating the columns 
        {
            System.out.print("*");
        }

        System.out.println();
    }

    System.out.println();
}
现在我们可以简单地将每个循环转换为它自己的递归函数,一个调用另一个:

public static void makeDesign(int n) 
{
    if (n > 0)
    {
        makeDesignRow(n);
        makeDesign(n - 1);
    }
    else
    {
        System.out.println();
    }
}

public static void makeDesignRow(int x)
{
    if (x > 0)
    {
        System.out.print("*");
        makeDesignRow(x - 1);
    }
    else
    {
        System.out.println();
    }
}
输出

通过
makeDesign()
一个10的参数,我们得到:

> java Main
**********
*********
********
*******
******
*****
****
***
**
*

> 

我认为,您的内部
for
循环甚至不会执行,因为
I
的最大值为4。您可能希望在问题中添加问题陈述。顺便说一句,用两个
for
循环来表达一个问题没有什么错。请展示您的尝试,并强调您遇到问题的地方。我们不会为你做你的工作,但如果你展示你所做的,我们会帮助你。它确实有效。它打印的内容如下:类似这样的内容,在屏幕上开始:***********我需要将相同的for循环转换为递归方法。
> java Main
**********
*********
********
*******
******
*****
****
***
**
*

>