public class Test {
	static void triangleLB(int a) { // Left-Bottom
		for (int i = 1; i <= a; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.printf("%3s", "*");
			}
			System.out.println();
		}
	}
	static void triangleLU(int a) {
		for (int i = a; i > 0; i--) {
			for (int j = 1; j <= i; j++) {
				System.out.printf("%3s", "*");
			}	
			System.out.println();
		}
	}
	static void triangleRU(int a) {
		for (int i = 0; i < a; i++) {
			for (int j = 0; j < a; j++) {
				if( i > j ) {
					System.out.print("   ");
				}else {
					System.out.print("  *");
				}
			}
			System.out.println();
		}
	}
	static void triangleRB(int a) {
		for (int i = a; i > 0; i--) {
			for (int j = 1; j <= a; j++) {
				if(j >= i) {
					System.out.print("  *");
				}else {
					System.out.print("   ");
				}
			}
			System.out.println();
		}
	}
	static void spira(int a) {
		for (int i = 1; i <= a; i++) {
			
			int x = (i-1)*2+1;
			int y = (a-1)*2+1;
			
			if(i == a) {
				for (int j = 0; j < y; j++) {
					System.out.printf("%3s", "*");
				}
			}else {
				for (int j = 0; j < a-i; j++) {
					System.out.printf("%3s", " ");
				}
				for (int j = 0; j < x; j++) {
					System.out.printf("%3s", "*");
				}
			}
			System.out.println();
		}
	}
	static void spira2(int a) {
		int n = 0;
		int j = 0;
		int x = 0;
		
		for (int i = 1; i <= a; i++) { 
			n = i + j;
			j++;
			x = a - i;
			for (int k = 0; k < x; k++) {
				System.out.print("   ");
			}
			for (int k = 0; k < n; k++) {
				System.out.printf("%3s","*");
			}
			System.out.println();
		}
	}
	public static void main(String[] args) {
		triangleLB(10);
		System.out.println();
		triangleLU(10);
		System.out.println();
		triangleRU(10);
		System.out.println();
		triangleRB(10);
		System.out.println();
		spira(5);
		System.out.println();
		spira2(5);
	}
}

 

+ Recent posts