Java uses the for loop: print the rules of right triangle / inverted right triangle / regular triangle / inverted regular triangle / rectangle / parallelogram / diamond

Posted by jamesm6162 on Tue, 21 Sep 2021 10:28:25 +0200

The newcomer said that after summing up the rules, it's a little fun~

Usage: nested for loop

1. The outer loop controls the number of execution rounds, and the inner loop controls the number of execution rounds

2. The outer loop controls the number of rows of the graph, and the inner loop controls the number of columns of this row

right triangle:

law:

1. Cycle condition   i value sets the specific side length

2. Inner for loop, loop condition   j   Value and outer loop   i   Value hook (print space plus *)

//right triangle
for (int i = 0; i <= 5; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(" *");//Print space plus*
            }System.out.println();//Line feed
        }

Inverted right triangle:

Law: (compare the right triangle above)

1. The outer for loop is completely opposite: the start condition is changed from 0 to 5, and the loop condition is' < '   Change to  '>' No., change the condition + + to--

2. The inner for loop remains unchanged (print space plus *)

//Inverted right triangle
for (int i = 5; i >= 0; i--){
            for (int j = 0; j <= i; j++){
                System.out.print(" *");//Print space plus*
            }System.out.println();//Line feed
        }

  Rectangle or square:

Law: (simplest)

1. the outer for as like as two peas for loops.

2. Inner layer print flag, outer layer print line feed (print space plus *)

//Rectangle or square
for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(" *");//Print space plus*
            }System.out.println();//Line feed
        }

Regular triangle:

law:

1. The outer for cycle is normal and unchanged

2. The first inner for loop is completely opposite to the outer for loop: the start condition is changed from 0 to 5, and the loop condition is' < '   Change to  '>' No., change the condition + + to--

  And cyclic conditions   j   Value and outer loop   i   Value hook

  (spaces are printed)

3. The second inner for loop, like the outer for loop, is only a loop condition   j   Value and outer loop   i   Value hook (print space plus *)

Note: two inner for loop conditions   j   Value and outer loop   i   All values are linked without assignment

//Regular triangle
for (int i = 0; i <= 5; i++) {
            for (int j = 5; j >= i ; j--) {
                System.out.print(" ");//Print spaces
            }
            for (int j = 0; j <= i; j++) {
                System.out.print(" *");//Print space plus*
            }System.out.println();//Line feed
        }

Inverted triangle:

Law: (compare the regular triangle above)

1. The outer for loop is completely opposite: the start condition 0 is changed to 5, and the loop condition '<' is marked   Change to  '>' No., change the condition + + to--

2. The two inner for loops remain unchanged

//Inverted triangle
for (int i = 5; i >= 0; i--) {
            for (int j = 5; j >= i ; j--) {
                System.out.print(" ");//Print spaces
            }
            for (int j = 0; j <= i; j++) {
                System.out.print(" *");//Print space plus*
            }System.out.println();//Line feed
        }

parallelogram:

Law: (compare regular triangle)

1., as like as two peas, the triangle is printed.

2. The only thing to change: the circulation condition of the second inner for loop, the J of the equilateral triangle<=   i   Change it to j < = 5 and change it to assignment

//Regular triangle
for (int i = 0; i <= 5; i++) {
            for (int j = 5; j >= i ; j--) {
                System.out.print(" ");//Print spaces
            }
            for (int j = 0; j <= i; j++) {
                System.out.print(" *");//Print space plus*
            }System.out.println();//Line feed
        }

Compare yourself  

//parallelogram
for (int i = 0; i <= 5; i++) {
            for (int j = 5; j >= i ; j--) {
                System.out.print(" ");//Print spaces
            }
            for (int j = 0; j <= 5; j++) {
                System.out.print(" *");//Print space plus*
            }System.out.println();//Line feed
        }

Diamond:

law:

1. It's an equilateral triangle at the top and an inverted equilateral triangle at the bottom. Just put them together

2. The only place to change: the outer for loop of the lower inverted equilateral triangle, the starting condition assignment minus 1, and the I of the upper equilateral triangle  = 5. Change to   i = 4   All right

Regular triangle  

//Regular triangle
for (int i = 0; i <= 5; i++) {
            for (int j = 5; j >= i ; j--) {
                System.out.print(" ");//Print spaces
            }
            for (int j = 0; j <= i; j++) {
                System.out.print(" *");//Print space plus*
            }System.out.println();//Line feed
        }

Splicing

Inverted triangle

//Inverted triangle
for (int i = 5; i >= 0; i--) {
            for (int j = 5; j >= i ; j--) {
                System.out.print(" ");//Print spaces
            }
            for (int j = 0; j <= i; j++) {
                System.out.print(" *");//Print space plus*
            }System.out.println();//Line feed
        }

  After only correction

//diamond
        //Regular triangle
        for (int i = 0; i <= 5; i++) {
            for (int j = 5; j >= i ; j--) {
                System.out.print(" ");//Print spaces
            }
            for (int j = 0; j <= i; j++) {
                System.out.print(" *");//Print space plus*
            }System.out.println();
        }
        //Inverted triangle
        for (int i = 4; i >= 0; i--) {
            for (int j = 5; j >= i ; j--) {
                System.out.print(" ");//Print spaces
            }
            for (int j = 0; j <= i; j++) {
                System.out.print(" *");//Print space plus*
            }System.out.println();
        }

I'm a newcomer. The above rules are my original summary and shared with other newcomers~

Topics: Java Algorithm Computer Graphics