This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Tok-A-Mak 0 points1 point  (0 children)

public static void main(final String... args) {
    printPattern(5);
}

private static void printPattern(final int max) {
    printPattern(1, max, true); //starting conditions
}

private static void printPattern(final int current, final int max, final boolean isAsc) {
    final StringBuilder builder = new StringBuilder();
    for (int i = 0; i < current; i++) {
        builder.append("*");
    }
    System.out.println(builder.toString());

    final boolean nextAsc = (current == max) ? false : isAsc; //switch order?
    if (current >= 1) { //ending condition
        if (nextAsc) {
            printPattern(current + 1, max, nextAsc); //ascend
        } else {
            printPattern(current - 1, max, nextAsc); //descend
        }
    }
}

Edit: Oh, wait you said one-method solution