행의 갯수를 입력받아서 다양한 모양으로 콘솔에 별을 찍어 보자!
1. 기본 별찍기

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("행의 개수를 입력하세요: ");
int rows = scanner.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
2. 왼쪽 삼각형 별 찍기

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("행의 개수를 입력하세요: ");
int rows = scanner.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
3. 오른쪽 삼각형 별 찍기

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("행의 개수를 입력하세요: ");
int rows = scanner.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = rows; j > 0; j--) {
if (i < j) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
}
}
4. 역삼각형 별 찍기

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("행의 개수를 입력하세요: ");
int rows = scanner.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = rows; j > 0; j--) {
if (i < j) {
System.out.print("*");
} else {
System.out.print("");
}
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("행의 개수를 입력하세요: ");
int rows = scanner.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = rows; j > i; j--) {
System.out.print("*");
}
System.out.println();
}
}
}
5. 오른쪽 역삼각형 별 찍기(공백 포함 별찍기)
여기서부터 난이도 중상 ★ ★ ★
역삼각형 별찍기에서 앞의 공백 부분은 안보여서 대시('-')로 대체했습니다.

package Baekjoon_test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("행의 개수를 입력하세요: ");
int rows = scanner.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("-");
}
for (int k = 0; k < rows - i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
5. 피라미드(공백 포함 별찍기)
난이도 상 ★★★★
앞의 공백은 잘 안보여서 대시('-')로 대체했습니다.

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("행의 개수를 입력하세요: ");
int rows = scanner.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows - i - 1; j++) { // 공백
System.out.print("-");
}
for (int k = 0; k < i * 2 + 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
6. 다이아몬드 모양(공백 포함 별찍기)
대망의 아름다운 별찍기 ㅋㅋㅋㅋㅋ
여기서부터 난이도 최상 ★★★★★
앞의 공백은 잘 안보여서 대시('-')로 대체했습니다.

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
for( int i = 0; i < 5; i++) {
for (int j = 1; j<5-i ; j++) {
System.out.print(" ");
}
for (int k = 0; k < i*2+1 ; k++) {
System.out.print("*");
}
System.out.println("");
}
for (int a = 1; a<5; a++) {
for (int b = 5; b > 5-a; b--) {
System.out.print(" ");
}
for (int c = 10; c > a*2+1; c--) {
System.out.print("*");
}
System.out.println("");
}
}
}
행 수(줄 수) 입력받아서 다이아몬드 별찍기 ▼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("행의 개수를 입력하세요: ");
int rows = scanner.nextInt();
// 윗 부분 출력
for (int i = 0; i < rows; i++) {
for (int j = 1; j < rows - i; j++) {
System.out.print(" ");
}
for (int k = 0; k < i * 2 + 1; k++) {
System.out.print("*");
}
System.out.println("");
}
// 아랫 부분 출력
for (int a = 1; a < rows; a++) {
for (int b = 0; b < a; b++) {
System.out.print(" ");
}
for (int c = 0; c < (rows - a) * 2 - 1; c++) {
System.out.print("*");
}
System.out.println("");
}
}
}
7. 그 외 다양한 모양으로 별찍기(다른 블로그 포스팅 북마크)
▼ 하트 모양, 육각 별 모양, 오각 별 모양 별 찍기 ▼



별을 찍어보자~ 2부
안녕하세요 루벤딕스입니다. 이번 포스팅은 저번 포스팅에 이은 별 찍기 2부인데 저번 포스팅보다 더 어려...
blog.naver.com
▼ 창문 모양 별찍기 ▼
[백준] 2447번 : 별 찍기 - 10 - JAVA [자바]
st-lab.tistory.com
'코딩테스트 > BaekJoon' 카테고리의 다른 글
| [백준] 10952번 : A+B - 5 ( 자바 / java ) (0) | 2024.05.08 |
|---|---|
| [백준] 2438번 : 별 찍기 - 2 ( 자바 / java ) (0) | 2024.04.23 |
| [백준] 2438번 : 별 찍기 - 1 ( 자바 / java ) (0) | 2024.04.23 |
| [백준] 11022번 : A+B-8 자바 / java (1) | 2024.04.23 |
| [백준] 11021번 : A+B-7 자바 / java (1) | 2024.04.23 |