-4
public void makeBox()
{
    output = "";

    for (int i = size; i > 0; i--) {
        for (int j = size; j > 0; j--) {
            output += "*";
        }
        for (int k = 1; k <= size; k++) {
            output += "#";
        }
        output += "\n";
    }
}

size is the variable for number of columns

when size = 4, the output should look like this:

****#
***##
**###
*####
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
golfer15
  • 1
  • 2

2 Answers2

1

You forgot to use i.

Your inner loop does the same thing independent of the value of i.

You need to fix this.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
0

You want to add "*" size - row times.

You want to add "#" row+1 times.

Where row is the current row number starting count at 0.

You should use StringBuilder because it's less costly than concatenation. See StringBuilder vs String concatenation in toString() in Java

Result would look like this:

StringBuilder sb = new StringBuilder();
for(int i = 0; i < size; i++)
{
     for(int j = 0; j < size - i; j++) sb.append("*");
     for(int k = 0; k < i+1; k++) sb.append("#");
     sb.append("\n");
}

Now you can do what you want with the result. To get it in string format:

String result = sb.toString();
Community
  • 1
  • 1
user3284549
  • 313
  • 3
  • 9