P
P
PRAIT2019-07-08 20:53:59
Java
PRAIT, 2019-07-08 20:53:59

How to understand why certain shapes are drawn in cycles?

Hello guys, I just can not understand the principle of the FOR loop. Rather, I understand how it works and what it is for, I know that there is an external and internal cycle. External reads vertical, internal horizontal.
But I can’t figure out how to implement all sorts of shapes, for example, rhombuses, squares with an empty middle, etc.

import java.util.Scanner;

public class Test {

  public static void main(String[] args) {
    try (Scanner tru = new Scanner(System.in)) {
      int count = 10;
      for (int i = 0; i < count; i++) {
        for (int k = 0; k < count; k++) {
          if (k == 0 || k == count - 1 || i == 0 || i == count - 1) {
            System.out.print("*");
          } else {
            System.out.print(" ");
          }
        }
        System.out.println(" ");
      }
    }
  }
}

Here we have 2 FOR loops
Outer with variable I
Inner with variable K
When the program starts, the outer loop gives 1 of its iterations, while the inner one gives all its iterations, right?
1 line
#**********
# - Outer loop
* - Inner
Next, K is less than COUNT? No, let's move on to the outer loop.
Next, the outer loop is executed again
. Next, the turn of the inner loop, there are spaces, this is the second line.
But why don't these spaces go, for example, in the first line?
if (k == 0 || k == count - 1 || i == 0 || i == count - 1)

I can't understand this line, here K == 0 but in the first line K is also zero, why are there no spaces?
k == count - 1 - it turns out if k == 9 spaces will be set? In general, something got confused ((
Please help me to scatter everything on the shelves, I will be grateful !!

#********#
# #
# #
# #
# #
# #
# #
# #
# #
#********#

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question