본문 바로가기

Language/Java

[JAVA] 별찍기

import java.util.Scanner;

public class StarPrint {

	public static void main(String[] args) {

		int max = 0;
		int start = 0;
		int blank = 0;

//		1. 3*4 네모

		System.out.println("1");
		for (int i = 0; i < 3; i++) { // 세로 3칸

			for (int j = 0; j < 4; j++) { // 가로 3칸

				System.out.print("*");

			}

			System.out.println();

		}

		System.out.println("\n---------------------\n");

//		2. 좌측 하단이 직각인 직각 삼각형

		System.out.println("2");
		max = 1; //별 갯수
		
		for (int i = 0; i < 3; i++) { // 세로 3칸

			for (int j = 0; j < max; j++) { //별 갯수만큼 반복

				System.out.print("*");

			}
			
			max++; //별 갯수 증가
			System.out.println();

		}

		System.out.println("\n---------------------\n");

//		3.우측 하단이 직각인 직각 삼각형
		
		System.out.println("3");
		start = 2; // 별이 찍히는 시작점

		for (int i = 0; i < 3; i++) { // 세로 3칸

			for (int j = 0; j < 3; j++) { // 가로 3칸

				if (j < start) { // j가 start 미만이면 공백, 이상이면 *
					System.out.print(" ");
				} else {
					System.out.print("*");
				}

			}

			start--; // start 감소
			System.out.println();

		}

		System.out.println("\n---------------------\n");

//		4. 높이가 3인 피라미드

		System.out.println("4");
	    max = 3; // 별의 끝점
		blank = 3; // 공백 갯수 + 1

		for (int i = 0; i < 3; i++) {

			for (int j = 0; j < max; j++) {

				if (j < blank - 1) { // j가 공백 갯수보다 작으면 공백, 크면 *
					System.out.print(" ");
				} else {
					System.out.print("*");
				}

			}

			blank--; // 공백 수 감소
			max++; // 별의 끝점 + 1
			System.out.println();

		}

		System.out.println("\n---------------------\n");

//		5. 높이가 5인 마름모

		System.out.println("5");

		for (int i = 0; i < 5; i++) { // 세로 5칸

			for (int j = 0; j < 5; j++) { // 가로 5칸

				if (i + j < 2) { // 왼쪽 윗부분 공백

					System.out.print(" ");

				} else if (j - i > 2) { // 오른쪽 윗부분 공백

					System.out.print(" ");

				} else if (i - j > 2) { // 왼쪽 아랫부분 공백

					System.out.print(" ");

				} else if (i + j > 6) { // 오른쪽 아랫부분 공백

					System.out.print(" ");

				} else { // 나머지 부분 *

					System.out.print("*");

				}

			}

			System.out.println();

		}

		System.out.println("\n---------------------\n");

//		6. 높이가 n인 마름모(n은 홀수)
//      6-1 모서리 자르기
		System.out.println("6");

		int height = 0; // 세로를 입력받을 변수
		
		Scanner sc = new Scanner(System.in);
		height = sc.nextInt();
		
		System.out.println();

		for (int i = 0; i < height; i++) {

			for (int j = 0; j < height; j++) {

				if (i + j <= (height/2)-1) { // 왼쪽 윗부분 공백, 가로 세로의 합이 세로의 절반 -1 보다 이하일 떄

					System.out.print(" ");

				} else if (j - i >= (height+1)/2) { // 오른쪽 윗부분 공백, 가로에서 세로를 뺸 값이 세로+1의 절반보다 이상일 떄

					System.out.print(" ");

				} else if (i - j >= (height+1)/2) { // 세로에서 가로를 뺀 값이 세로+1의 절반 이상일 떄

					System.out.print(" ");

				} else if (i + j >= height + (height/2)) { // 가로, 세로의 합이 세로에 세로의 절반을 더한 값 이상일 떄

					System.out.print(" ");

				} else { // 나머지는 *

					System.out.print("*");

				}

			}
			

			System.out.println();

		}
		sc.close(); // Scanner 닫기

		System.out.println("\n---------------------\n");
       //6-2 중간까지는 증가 이후로는 감소
//		Scanner sc = new Scanner(System.in);
//		int h = sc.nextInt();
//
//
//		int max = (h - 1) / 2; 
//		int blank = (h + 1) / 2; 
//
//		for (int i = 0; i < h; i++) {
//
//			for (int j = 0; j <= max; j++) {
//
//				if (j < blank - 1) { 
//					System.out.print(" ");
//				} else {
//					System.out.print("*");
//				}
//
//			}
//
//			if (i < (h - 1) / 2) {
//
//				blank--;
//				max++; 
//
//			} else {
//
//				blank++; 
//				max--; 
//				
//			}
//
//			System.out.println();

		}

	}

}