i can't get this recursion method to work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joder2006
    New Member
    • Dec 2006
    • 12

    #1

    i can't get this recursion method to work

    i have a program where a user can input a number and that number creates a pattern of asterisks for example if the number is four it looks like this


    *
    **
    *
    ****
    *
    **
    *

    and eight looks like this

    *
    **
    *
    ****
    *
    **
    *
    ********
    *
    **
    *
    ****
    *
    **
    *

    the problems is i can't figure out how to get the spacing to work
    here is my code


    package recursion;

    public class recurse
    { private String temp = "";
    private int line = 0;


    public void tower(int n)
    {
    if (n == 1)
    {



    temp += "*";
    System.out.prin tln(temp);
    temp = ""; }

    else
    {

    tower((n+1)/2);



    for (int x = 0; x < n; x++)
    { temp += "*"; }
    space = n;
    line++;
    System.out.prin tln(temp);
    temp = "";

    tower((n+1)/2);



    }

    }


    }


    when i run the program with the number being 8 i get this
    *
    **
    *
    ****
    *
    **
    *
    ********
    *
    **
    *
    ****
    *
    **
    *

    I think the answer is simple but i dunno
  • joder2006
    New Member
    • Dec 2006
    • 12

    #2
    nevermind i figured it out

    Comment

    Working...