자바에는 출력을 위해 System.out.pruint, println, printf 등 다양한 방법을 사용하여 출력할 수 있다.
그중 printf를 사용하면 쉽게 역삼각형을 그릴 수 있다.
printf("%숫자s\n",star)의 의미는 숫자만큼의 범위를 잡고 뒤에서부터 내용을 채우는 것이다.
이를 사용해서 역삼각형을 그리는 예제를 구현해보겠다.
흐름
우선 별 그리기의 흐름을 정리해보자.
1. 몇 층짜리 별을 그릴지 입력 2. 해당 층수만큼 반복하며 별의 개수 증가 3. 출력이 완료될 경우 종료
구현
import java.util.*;
public class Star {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int replayCount = 0;
String star = "*";
System.out.print("몇층짜리 별을 그리겠습니까?");
replayCount = sc.nextInt();
for(int i=0;i<replayCount;i++) {
System.out.printf("%"+replayCount+"s\n",star);
star += "*";
}
}
}
import java.util.Scanner;
public class StarExam {
public static final String STAR = "*";
// star-1번 과제
void starOne() {
for (int i = 1; i <= 12; i++) {
System.out.print(STAR);
if (i % 4 == 0)
System.out.println(); // 4개씩 끊어서 출력
}
}
// star-2번 과제
void starTwo() {
for (int i = 1; i < 4; i++) {// 행
for (int j = 0; j < i; j++) { // i행일때 별 개수 i개 입력
System.out.print(STAR);
}
System.out.println();
}
}
// star-3번 과제
void starThree() {
int fr = 2;
for (int i = 1; i < 4; i++) { // 행
for (int j = 1; j <= 3; j++) {// 열
if (j <= fr) { // 공백
System.out.print(" ");
} else { // 별
System.out.print(STAR);
}
}
fr--;
System.out.println();
}
}
// star-4번 과제
void starFour() {
int fr = 2, ba = 3;
for (int i = 1; i <= 3; i++) {// 행
for (int j = 1; j <= 6; j++) {// 열
if (j <= fr) {// 공백 삽입
System.out.print(" ");
} else if (j > ba) {// 공백 삽입
System.out.print(" ");
} else { // 별 삽입
System.out.print(STAR);
}
}
fr--;
ba++;
System.out.println();
}
}
// star-5번 과제
void starFive() {
int fr = 3, ba = 3; // 공백이 들어가는 조건 fr앞 공백, ba뒷 공백 조건으로 사용
for (int i = 1; i <= 5; i++) { // 5번 반복
for (int j = 1; j <= 5; j++) { // 5열 생성
if (j < fr) { // 각 열을 fr과 비교해 공백이 들어갈 부분에 공백 삽입, 중간열을 기준으로 앞 공백
System.out.print(" ");
} else if (j > ba) { // 각 열을 ba와 비교해 공백 삽입, 중간열을 기준으로 뒷 공백
System.out.print(" ");
} else { // 별이 들어갈 곳
System.out.print(STAR);
}
}
if (i < 3) { // 중간층까지 별 개수를 늘리도록 조건을 증가 및 감소
fr--;
ba++;
} else { // 중간층 이후 별 개수를 줄이도록 조건을 증가 및 감소
fr++;
ba--;
}
System.out.println();
}
}
// star-6번 과제
void starSix() {
Scanner sc = new Scanner(System.in);
System.out.print("몇층까지 하겠습니까? (홀수만 가능):");
int re = sc.nextInt(); // 층 입력
if (re % 2 == 0) {
System.out.println("홀수를 입력해주세요.");
} else {
int fr = re / 2 + 1, ba = re / 2 + 1; // 공백이 들어가는 조건 fr앞 공백, ba뒷 공백 조건으로 사용
for (int i = 1; i <= re; i++) { // i를 입력받은 re층 만큼 반복
for (int j = 1; j <= re; j++) { // j는 입력받은 re의 열 만큼 반복
if (j < fr) { // 앞부분이 공백이 들어가는 조건일 경우 공백 삽입
System.out.print(" ");
} else if (j > ba) { // 뒷 부분이 공백이 들어가는 조건일 경우 공백 삽입
System.out.print(" ");
} else {
System.out.print(STAR);
}
}
if (i < re / 2 + 1) { // 중간층까지 별 개수를 늘리도록 조건을 증가 및 감소
fr--;
ba++;
} else { // 중간층 이후 별 개수를 줄이도록 조건을 증가 및 감소
fr++;
ba--;
}
System.out.println();
}
}
}
public static void main(String[] args) {
StarExam starexam;
starexam = new StarExam(); // static이 아닐 경우 new를 해야한다.
// 클래스로 받아 오는것들은 static이다
Scanner sc;
sc = new Scanner(System.in);
int num1;
String replay;
do {
System.out.print("1~6 입력");
num1 = sc.nextInt();
switch (num1) { // 번호에 따라 case를 나눈다
case 1:
starexam.starOne();
break;
case 2:
starexam.starTwo();
break;
case 3:
starexam.starThree();
break;
case 4:
starexam.starFour();
break;
case 5:
starexam.starFive();
break;
case 6:
starexam.starSix();
break;
default: // 다른 값이 들어올 경우
System.out.println("잘못 입력했습니다.");
}
System.out.println("다시 하시겠습니까? (Y/N)");
replay = sc.next(); // 비교할 값을 입력받음
} while (replay.equals("y") || replay.equals("Y"));
// replay가 "y"나 "Y"일 경우 true값으로 do를 반복
System.out.println("끝났습니다."); // false일 경우 출력 후 종료
}
}