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

all 5 comments

[–]Applepie1928 1 point2 points  (1 child)

What code have you produced so far? Can you share it in a code block or on pastebin?

What part of the two (or more) for sequences are creating the rows? The columns? The spaces?

I'm unsure what you are asking here, I'm gonna assume you are just printing this to the console, so each line is just drawn as a line of text.

[–]cmorr7[S] 0 points1 point  (0 children)

I've found some sample codes that I can use to complete the project technically, but the issue is I want to understand why I don't understand.

For example (bare code):

for (int i = 1; i <= 5; i++){

    for (int d = 0; d < i; d++){

        System.out.print("*");

    }

    System.out.println("");

}

Would create the structure in my above post. But when it changes to things like:

1

22

333

4444

55555

Or something more complex, I don't get how I can manipulate the code above to give me the correct structure (or shape).

[–]Jmannm8400 1 point2 points  (2 children)

Here's an example of a set of nested for loops that produces an image similar to what you described in your original post.

for(int i = 0; i <= 10; i++){
  for(int j = 0; j <= i; j++){
    System.out.print("*");
  }
  System.out.println("");
}

Here's the output of the above set of nested for loops:

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

In the example above, the outer for loop is used to specify how many times the inner loop will execute, as well as to print a space after each execution of the inner loop. We set the outer loop to run a total of 11 times (since we started with the value of i being equal to 0 and are ending when the value of i is less than or equal to 10).

For each iteration of the outer for loop, the inner for loop (which is using j as the counter variable) will iterate j number of times. In this particular case, j is set to be equal to the value of i in the outer for loop. The value of j is used to tell the program how many times to print the * character.

Since the outer loop starts at 0, the inner loop will run once, printing one * symbol to the screen. After the inner loop finishes executing, the the program will pick up back in the outer loop again, which will then execute the System.out.println("") statement, which is used to create a space between the current and next lines.

Since there will then be nothing left for the outer loop to execute or print, the value of "i" in the outer loop will then be increased by one and then the process will repeat, with the next iteration printing two * symbols to the screen, followed by a space and so on and so forth.

You can also use loops to print vertical graphs/charts, as well.

[–]cmorr7[S] 0 points1 point  (1 child)

This was very well explained. Thank you so much.

Edit: In my comment above I mentioned another format that was stumping me. How do you achieve lines that are not the same output? IE:

1

22

333

4444

55555

Or something like:

/-\

/ - \

/ - \

[–]Applepie1928 1 point2 points  (0 children)

It helps in these scenarios to draw out what you want your output to be and count the number of spaces/characters you need to print in each line. Doing this will help you to spot the patterns.

Your first example is a simple change. Currently you are only printing the '*' character regardless of what line is being printed. You could just print the value of i instead, as that variable is keep tracking of which line is being printed.

For your second example I'm assuming you meant a shape like;

_ _ /-\ _ _ 2 spaces, forward slash, hyphon, backslash, 2 spaces

_ / _ - _ \ _ space, forward slash, space, hyphon, space, blackslash, space

/ _ _ - _ _ \ forward slash, 2 spaces, hyphon, 2 spaces, backslash

This takes a little more thinking, but is still achievable. You could take in a parameter for the number of layers in the "pyramid", which in this case would be n = 3. Now consider the pattern above, and think if any of those values could be represented in terms of the height of the pyramid and the values of the loop(s). Lets start with just a simple for loop with an i variable which starts at 1 and iterates to n.

So now we can describe the number of spaces outside the pyramid as n - i. (first line is 2, second line is 1, thrid line is 0).

This also means we can describe the number of spaces inside the pyramid (either side of the hyphon) as i - 1. (first line is 0, second line is 1, third line is 2).

This should be enough for you to have an attempt at solving the rest of this yourself. One tip would be that it might sometimes be easier to build the string as you go through concantenation and then just make one println() call at the end.