public void printStars(int level) {
for (int one = level; one >= 1; one--) {
for (int two = one; two <= level; two++) {
System.out.print("*");
}
System.out.println();
}
}
I am trying to make something that looks like this:
*
***
*****
*******
*********
Currently I am getting about half of the correct diagram all left aligned.
I tried to incorporate printf, but I realized that it wouldn’t work because the value of level cannot be transferred to the printf method. I was also wondering if there was a way to set the longest segment (the bottom) equal to (2 * level) – 1 stars long and apply some sort of formatting on that to get the answer?
Print appropriate number of spaces ahead
Explanation:
if there are N levels in total, and last line doens’t have any space. => N-1 levels will have spaces with N-1 in first line, N-2 in 2nd line, so on.. 1 in (N-1)th line.
Number of stars: (level-one) gives the line you are printing minus 1, because, initially when one = level (one-level = 0), its 1st line, next its (level-one) = 1, because one is decreased. & in each line you have to print, 2*X +1 *’s,