728x90
반응형
package chap5;
import java.util.Scanner;
/*
* 이등변 삼각형을 출력하는 프로그램 작성하기
[결과]
삼각형의 높이 입력 : 3
*
***
*****
l b
1 => 1
2 => 3
3 => 5
4 => 7
2*l - 1 => b
공백 : 높이-행의값
* : 2* 행의값 - 1
*/
public class Test0307_2 {
public static void main(String[] args) {
System.out.println("삼각형의 높이를 입력하세요");
Scanner scan = new Scanner(System.in);
int len = scan.nextInt();
for(int i=1;i<=len;i++) {
for(int j=1;j<=len-i;j++) {
System.out.print(" ");
}
for(int j=1;j<=2*i-1;j++) {
System.out.print("*");
}
System.out.println();
}
int bottom = len * 2 - 1;
int m = bottom / 2;
for (int i = 0; i < len; i++) {
for (int j = 0; j < bottom; j++) {
if (j >= m - i && j <= m + i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
System.out.println();
System.out.println("역삼각형");
m = bottom/2;
for(int i=0;i<len;i++) {
for(int j=0;j<bottom - i;j++) {
if(j>=i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
728x90
반응형