Tag Archives: Experiment II—Basic Exercises of JAVA Grammar

Java — for loop printing graphics (loop structure)

Java — for loop print graphics (loop structure)

Description
through the use of double for loop statements, print the following graph:

submit
Sample
the Output

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		int i, j, m, t = 1;// m为后半截控制空格个数的变量
		for (i = 1; i <= 4; i++) {
			for (j = 1; j <= 4 - i; j++) {// 空格
				System.out.print(" ");
			}
			for (j = 1; j <= 2 * i - 1; j++) {// 星号
				System.out.print("*");
			}
			System.out.println();
		}
		for (i = 3; i >= 1; i--) {
			for (j = 1; j <= t; j++) {// 空格
				System.out.print(" ");
			}
			for (j = 1; j <= i * 2 - 1; j++) {
				System.out.print("*");// 星号
			}
			System.out.println();
			t++;
		}
	}

}