Java (for loop): graphics like pyramids

Pyramid and other graphics.
This kind of graphics is actually mathematical in nature. When the image is drawn, it is easy to write the
first shape by observing the number of lines and each line.
pyramid
This is actually a square to a right angle and a square shape. * Graphics of the pyramid

System.out.println("=============第一个图形===========");
		int i, j;
		for( i=1; i<=9; i++) {
    
    
			for( j=1; j<=9-i; j++) {
    
    
				System.out.print(" ");
			}
			for( j=1; j<=2*i-1; j++) {
    
    
				System.out.print("*");
			}
			System.out.println();
		}

The second figure When
Half diamond
writing this figure, it is the same as doing mathematical geometry problems. Divide him into two rectangles, each of which is divided into triangles with spaces and triangles with asterisks.

System.out.println("=============第二个图形===========");
		int m, n;
		for( m=1; m<=9; m++) {
    
    
			for( n=1; n<=9-m; n++) {
    
    
				System.out.print(" ");
			}
			for( n=1; n<=m; n++) {
    
    
				System.out.print("*");
			}
			System.out.println();
		}
		for( m=1; m<=9; m++) {
    
    
			for( n=1; n<=m; n++) {
    
    
				System.out.print(" ");
			}
			for( n=1; n<=9-m; n++) {
    
    
				System.out.print("*");
			}
			System.out.println();
		}

The third figure The
Windmill-like graphics
third figure is actually the second figure, with a rectangle formed by adding a space at the bottom.

System.out.println("=============第三个图形===========");
		int p, q;
		for( p=1; p<=9; p++) {
    
    
			for( q=1; q<=9-p; q++) {
    
    
				System.out.print(" ");
			}
			for( q=1; q<=p; q++) {
    
    
				System.out.print("*");
			}
			System.out.println();
		}
		for( p=1; p<=9; p++) {
    
    
			for( q=1; q<=9; q++) {
    
    
				System.out.print(" ");
			}
			for( q=1; q<=10-p; q++) {
    
    
				System.out.print("*");
			}
			System.out.println();
		}

In summary, homework problems similar to those in schools are actually math problems. .

Guess you like

Origin blog.csdn.net/qq_45884783/article/details/104866571