류지나 2021. 11. 16. 17:10
플로이드의 삼각형

 

public class Main {
    public static void main(String[] args) {
        ShapePrinter printer = new ShapePrinter();

        // 테스트
        printer.printFloydsPyramid(3);
        System.out.println("----------");
        printer.printFloydsPyramid(5);
        System.out.println("----------");
        printer.printFloydsPyramid(15);
    }
}



public class ShapePrinter {
    public void printFloydsPyramid(int height) {        
        int count = 1;
        int length = String.valueOf(height * (height + 1) / 2).length();
        
        for (int i = 1; i <= height; i++) {
            for (int j = 1; j <= i; j++) {
                if (length == 2) {
                    if (count < 10) {
                        System.out.print(" ");
                    } else if(count >= 10) {
                      //  System.out.print("");
                    }
                }
                if (length == 3) {
                    if (count < 10) {
                        System.out.print("  ");
                    } else if (count < 100) {
                        System.out.print(" ");
                    } else {
                        System.out.print("");
                    }
                }
                for (int l = 1; l <= j; l++) {
                    System.out.print(count);
                    System.out.print(" ");
                    count++;
                    break;
                }
            }
            System.out.println();
        }
    }
}
public class FloydsTrianglePrinter printTriangle(){
    public void printTriangle(int height){
	
	//숫자자리 구하기
	int length = String.valueOf(height*(height+1)/2).length();
	//인쇄될 수
	int number = 1;
	//반복문1 - 행을 바꿈
	for (int row=1;row<=height;row++){
		//한 행에서 인쇄될 내용을 담는 문자열
		String line="";
		//반복문2 - 인쇄될 내용을 담는 문자열선언
		for (int col=1; col<=row; col++){
	        //반복문3 - 한열 안에서 자리수를 마주추는 반복문3
			for(int i = String.valueOf(number).length();i<length;i++){
			    line += " ";
			}
			//실제 인쇄될 수를 문자열에 더하고 한칸 띄워주기
			line += (number + " ");
	        number+=1;
		    
		}
		System.out.println(line);
	}