Java с ромбовидным узором с использованием рекурсииJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Java с ромбовидным узором с использованием рекурсии

Сообщение Anonymous »


I'm having some problems understanding how the recursion works and i have this question asking me to print a diamond pattern using recursion. after a while i got to something close to the solution i think but my output is this:

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * when it should be this:

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * public class DiamondPattern { public static void main(String[] args) { diamond(6, 6); } public static void repeatlyPrint(int amount, String pattern) { if (amount > 0) { // print a pattern once System.out.print(pattern); // recursively call the function itself repeatlyPrint(amount - 1, pattern); } } public static void diamond(int totalHeight, int currentRow) { printSpaces(totalHeight - currentRow); // print spaces (outside of diamond) repeatlyPrint(currentRow , "* "); // print asterisks and spaces System.out.println(); if (currentRow > 1) { diamond(totalHeight, currentRow -1); } printSpaces(totalHeight - currentRow); // print spaces repeatlyPrint( currentRow, "* "); // print asterisks and spaces System.out.println(); } private static void printSpaces(int count) { for (int i = 0; i < count; i++) { System.out.print(" "); } } } i found a question really similar with a good explanation but i'm still really confuse that's why i'm opening this question, please close if not allow but i'm really stuck.

if someone can lead me in the right direction on where am i going wrong here would be great.


Источник: https://stackoverflow.com/questions/780 ... -recursion
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»