728x90
반응형
문제 1
*
**
***
****
*****
- 반복문을 이용하라.
- ' '(공백)과 '*'(별)을 이용해 위 그림과 같이 출력하라.
▶ 코드
int max=5;
int row,column;
for(row=0;row<max;row++){
for(column=0;column<(max-row)-1;column++){
System.out.print(" ");
}
for(column=0;column<row+1;column++){
System.out.print("*");
}
System.out.println();//줄바꿈
}
▶ 출력 결과
*
**
***
****
*****
문제 2
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
- 반복문을 이용하라.
- ' '(공백)과 '*'(별)을 이용해 위 그림과 같이 출력하라.
▶ 코드
int i,j,k;
int max=5;//5줄을 기준으로 출력하기 위해 미리 선언
//5줄 출력
for(i=0;i<max;i++){//0~4
for(j=0;j<max-i;j++){
System.out.print(" ");
}
for(k=0;k<i*2+1;k++){
System.out.print("*");
}
System.out.println(); //줄바꿈
}////out_for1
//6줄 출력
for(i=max;i>=0;i--)//5~0
{
for(j=0;j<max-i;j++){
System.out.print(" ");
}
for(k=0;k<i*2+1;k++){
System.out.print("*");
}
System.out.println(); //줄바꿈
}//out_for2
▶ 출력 결과
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
문제 3
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
- 반복문을 이용하라.
- ' '(공백)과 '*'(별)을 이용해 위 그림과 같이 출력하라.
▶ 코드
int max=5;
int row, column;
//위
for(row=0;row<max;row++){
for(column=0;column<max-row;column++){
System.out.print("*");
}//inner_for1
for(column=0;column<(row*2)+1;column++){
System.out.print(" ");
}//inner_for2
for(column=0;column<max-row;column++){
System.out.print("*");
}//inner_for3
System.out.println(); //줄바꿈
}//out_for1
//아래
for(row=max-1;row>=0;row--){
for(column=0;column<max-row;column++){
System.out.print("*");
}//inner_for1
for(column=0;column<(row*2)+1;column++){
System.out.print(" ");
}//inner_for2
for(column=0;column<max-row;column++){
System.out.print("*");
}//inner_for3
System.out.println(); //줄바꿈
}//out_for2
▶ 출력 결과
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
728x90
반응형
'HRD_훈련 > 실습' 카테고리의 다른 글
[JAVA] UP&DOWN 답 맞추기 (0) | 2023.05.25 |
---|---|
[JAVA] Random 함수를 이용한 구구단 답 맞추기 (0) | 2023.05.25 |
[JAVA] Math.random()을 이용한 LOTTO (0) | 2023.05.23 |
[JAVA] 주민등록번호를 입력 받아 남자인지 여자인지 판별 (0) | 2023.05.22 |
[JAVA] 조건문을 이용해 학점 구하기 (0) | 2023.05.21 |