Java multiple loop printing graphics

Java multiple loop printing graphics

I think that when using Java multiple loops to print graphics, the outer loop is used to control the number of lines of the target shape, and the most important thing is to write the loop conditions of the inner loop, so that the target shape can be drawn.

Example: Right triangle The
code is as follows:

//外循环——控制输出行数
for (int i = 1;i <= 5;i++){
    
    
			//内循环——控制每行输出的*号个数
            for (int j = 1;j <= i;j++){
    
    
                System.out.print("*");
            }
            System.out.println();
        }

The console output result is: in the
Right triangle




same way, an inverted right triangle can be obtained, and the code is as follows:

for (int i=1;i <=5;i++){
    
    
            for (int j =1;j <= 6-i;j++){
    
    
                System.out.print("*");
            }
            System.out.println();
        }

The console output is:
Inverted right triangle




Now that we know how to print a triangle, how should we print an isosceles triangle?
Isosceles triangle



As long as the triangle is printed, and how much space should be left in the first column of each row, an isosceles triangle can be realized.

Therefore, we add a loop to the inner loop to achieve the indentation of each line.
code show as below:

for (int i=1;i <=5;i++){
    
    
			//输出空格,实现行缩进
            for (int j=1;j <= 5-i;j++){
    
    
                System.out.print(" ");
            }
            //输出*号,打印图形
            for (int j=1;j <= 2*i-1;j++){
    
    
                System.out.print("*");
            }
            System.out.println();
        }

In the code above, the loop conditions of the two loops in the inner loop are calculated based on the shape of the isosceles triangle.

Idea: The
first line outputs a * sign, the second line outputs three * signs, and the third line outputs five * signs...in an arithmetic increment, and the tolerance D is 2.
Therefore, we can use the formula X+i * D = Y to calculate the range of the loop condition, and Y represents the number of output * signs.
Where X is an unknown number, when i is equal to 1, substitute into the formula to get X+2 = 1, then X = -1, then this formula is Y = 2 * i-1, so the loop condition for controlling the number of * is
j <= 2 * i -1.
To realize an isosceles triangle, it is also necessary to introduce a formula Xi*D=Y to calculate the range of the calculation cycle condition, and Y represents the number of "blanks" output. At this time, the tolerance D of each line of "space" is 1, where X is an unknown number. When i is equal to 1, substitute the formula to get X-1 = 4, then X = 5, then this formula is Y = 5-i, so control The loop condition for the number of "blanks" is
j <= 5-i.



After understanding the above two formulas, we can print the rhombus.
diamond
This is a rhombus formed by splicing two triangles. The code is as follows:

		//打印上半部分三角形
		for (int i=1;i <=5;i++){
    
    
            for (int j=1;j <= 5-i;j++){
    
    
                System.out.print(" ");
            }
            for (int j=1;j <= 2*i-1;j++){
    
    
                System.out.print("*");
            }
            System.out.println();
        }
        //打印下半部分三角形
        for (int i=1;i <=4;i++){
    
    
            for (int j =1;j <= i;j++){
    
    
                System.out.print(" ");
            }
            for (int j =1;j <= 9-2*i;j++){
    
    
                System.out.print("*");
            }
            System.out.println();
        }




Obviously, this method is too cumbersome to use two triangles to join the rhombus, and it does not reflect the advantages of circulation.

We only need to add another loop variable k to control the loop change in the lower half when the diamond has finished printing the upper half.

for (int i = 1,k = 1;k <= 9;i = (k++<5 ? ++i : --i)){
    
    
            for (int j = 1;j <= 5-i;j++){
    
    
                System.out.print(" ");
            }
            for (int j = 1;j <= 2*i-1;j++){
    
    
                System.out.print("*");
            }
            System.out.println();
        }

Or give the loop variable a fixed range, and add the absolute value to control the lower half of the loop change.

for (int i = -4;i <= 4;i++){
    
    
            for (int j = 1;j <= Math.abs(i);j++){
    
    
                System.out.print(" ");
            }
            for (int j = 1;j <=9-2*Math.abs(i);j++){
    
    
                System.out.print("*");
            }
            System.out.println();
        }





Using the method introduced above, we can also print out the hourglass figure, which is simply a splicing of the vertices of two triangles.
code show as below:

for (int i = -4;i <= 4;i++){
    
    
            for (int j = 1;j <= 4-Math.abs(i);j++){
    
    
                System.out.print(" ");
            }
            for (int j = 1;j <= 1+2*Math.abs(i);j++){
    
    
                System.out.print("*");
            }
            System.out.println();
        }

The result of the console output is:
Hourglass





hollowed out graphics
Insert picture description here
This is to hollow out the middle of the hourglass graphics. The principle is to output only the first row, the last row, the first column of each row, and the last column of each row when printing the * sign. code show as below:

for (int i = -4;i <= 4;i++){
    
    
            for (int j = 1;j <= 4-Math.abs(i);j++){
    
    
                System.out.print(" ");
            }
            for (int j = 1;j <= 1+2*Math.abs(i);j++){
    
    
                boolean is = i==-4 || i ==4 || j==1 || j==1+2*Math.abs(i);
                System.out.print(is ? "*":" ");

            }
            System.out.println();
        }

Add a Boolean value to determine, it can be achieved.

Guess you like

Origin blog.csdn.net/weixin_48482704/article/details/108854226