게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
층수 쌓기 / 반복문 없음
게시물ID : programmer_830짧은주소 복사하기
작성자 :
추천 : 1
조회수 : 403회
댓글수 : 3개
등록시간 : 2014/01/27 00:32:54
층수 쌓기를 반복문 없이 해봤습니다. Java 코드입니당.


  1. import java.util.Scanner;
  2.  
  3. public class Pyramid {
  4.    
  5.     public static void main(String[] args) {
  6.         System.out.print("층수는?");
  7.         Scanner scanner = new Scanner(System.in);
  8.         try {
  9.             int numOfFloor = scanner.nextInt();
  10.             scanner.close();
  11.             Pyramid.printOut(numOfFloor, 11);
  12.         }
  13.         catch ( Exception e ) {
  14.             Pyramid.exitProgram();
  15.         }
  16.     }
  17.    
  18.     public static void printOut(int row, int y, int x) {
  19.         if( y > row ) {
  20.             return;
  21.         }
  22.         int center = (row * 2) / 2;
  23.         if( x == (center - y + 1) || x == (center + y - 1) ) {
  24.             System.out.print("*");
  25.         }
  26.         else {
  27.             System.out.print("-");
  28.         }
  29.        
  30.         if( x < (row * 2) ) {
  31.             x++;
  32.         }
  33.         if( x >= (row * 2) ) {
  34.             System.out.println("");
  35.             y++;
  36.             x = 1;
  37.         }
  38.         Pyramid.printOut(row, y, x);
  39.     }
  40.    
  41.     public static void exitProgram() {
  42.         System.err.println("숫자를 입력받는 데 실패했습니다.");
  43.         System.exit(1);
  44.     }
  45. }
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호