Я вижу первый шаг как правильное переопределение 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();
}
Следующий шаг - заставить оба цикла считать до 1, чтобы упростить рекурсию, когда придет время:
public static void makeDesign(int n)
{
for (int i = n; i > 0; i--) // For loop is the one creating the rows
{
for (int x = i; x > 0; 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();
}
}
OUTPUT
Передав makeDesign()
аргумент 10, мы получим:
> java Main
**********
*********
********
*******
******
*****
****
***
**
*
>