층수 쌓기를 반복문 없이 해봤습니다. Java 코드입니당.
-
import java.util.Scanner;
-
-
public class Pyramid {
-
-
public static void main(String[] args) {
-
System.out.print("층수는?");
-
Scanner scanner = new Scanner(System.in);
-
try {
-
int numOfFloor = scanner.nextInt();
-
scanner.close();
-
Pyramid.printOut(numOfFloor, 1, 1);
-
}
-
catch ( Exception e ) {
-
Pyramid.exitProgram();
-
}
-
}
-
-
public static void printOut(int row, int y, int x) {
-
if( y > row ) {
-
return;
-
}
-
int center = (row * 2) / 2;
-
if( x == (center - y + 1) || x == (center + y - 1) ) {
-
System.out.print("*");
-
}
-
else {
-
System.out.print("-");
-
}
-
-
if( x < (row * 2) ) {
-
x++;
-
}
-
if( x >= (row * 2) ) {
-
System.out.println("");
-
y++;
-
x = 1;
-
}
-
Pyramid.printOut(row, y, x);
-
}
-
-
public static void exitProgram() {
-
System.err.println("숫자를 입력받는 데 실패했습니다.");
-
System.exit(1);
-
}
-
}