메소드 명 : public void practice1( ){ }
다음과 같은 실행 예제를 구현하세요.
ex) 정수 입력 : 4
*
**
***
****
Public void practice1() {
Scanner sc = new Scanner(System.in);
System.out.println("정수 입력 : ");
int input = sc.nextInt();
for(int row = 1; row <= input; row++) {
for(int col = 1; col <= row; col++) {
System.out.print("*");
}
System.out.println(); // 한 행의 모든 열 출력 끝나면 줄바꿈(다음 행)
}
}
메소드 명 : public void practice2( ){ }
다음과 같은 실행 예제를 구현하세요.
ex) 정수 입력 : 4
****
***
**
*
행(row)은 입력한 input만큼
(단, 안쪽 for문을 생각해서 1씩 감소하는 형태로 작성)
열(col)은 1부터 현재형(row) 범위만큼 반복
Public void practice2() {
Scanner sc = new Scanner(System.in);
System.out.println("정수 입력 : ");
int input = sc.nextInt();
for(int row = input; row >= 1; row--) {
// 내가 입력한 input 값을 row에 대입해 반복할 때마다 1씩 감소
// 4 3 2 1..
for(int col = 1; col <= row; col++) {
System.out.print("*");
}
System.out.println();
}
}
메소드 명 : public void practice3( ){ }
다음과 같은 실행 예제를 구현하세요.
ex) 정수 입력 : 4
*
**
***
****
공백 부분이 input보다 1 작은 수에서 시작해
1씩 감소하는 것처럼 보임
* 부분은 현재행(row)만큼 찍힌 것으로 보임
Public void practice3() {
Scanner sc = new Scanner(System.in);
System.out.println("정수 입력 : ");
int input = sc.nextInt();
for(int row = 1; row <= input; row++) {
// 공백 부분 입력할 for문
for(int i = input-1; i >= row; i--) {
System.out.print(" ");
}
// 현재 row만큼 반복하여 * 출력해줄 for문
for(int col = 1; col <= row; col++) {
System.out.print("*");
}
System.out.println();
}
}
메소드 명 : public void practice4( ){ }
다음과 같은 실행 예제를 구현하세요.
ex) 정수 입력 : 3
*
**
*** (여기까지 위쪽 삼각형)
**
*
Public void practice4() {
Scanner sc = new Scanner(System.in);
System.out.println("정수 입력 : ");
int input = sc.nextInt();
// 위쪽 삼각형
for(int row = 1; row <= input; row++) {
for(int col = 1; col <= row; col++) {
System.out.print("*");
}
System.out.println();
}
// 아래쪽 삼각형
for(int row = input-1; row >= 1; row--) {
for(int col = 1; col <= row; col++) {
System.out.print("*");
}
System.out.println();
}
}
메소드 명 : public void practice5( ){ }
다음과 같은 실행 예제를 구현하세요.
ex) 정수 입력 : 4 → 왼쪽에서부터 공백 포함
* // input(4) + row(1) - 1 == 4 -> 한 행에 열 (col)이 4번 반복하여 공백이나 * 찍음
*** // input(4) + row(2) - 1 == 5 -> 한 행에 열 (col)이 5번
***** // input(4) + row(3) - 1 == 6 -> 한 행에 열 (col)이 6번
******* // input(4) + row(4) - 1 == 7 -> 한 행에 열 (col)이 7번
Public void practice5() {
Scanner sc = new Scanner(System.in);
System.out.println("정수 입력 : ");
int input = sc.nextInt();
// 1부터 시작해서 input까지 1씩 증가
for(int row = 1; row <= input; row++) {
for(int col = 1; col <= input + row - 1; col++) {
// [1-1턴] 1 <= ( 4 + 1 - 1) 4
// [1-2턴] 2 <= 4
// [1-3턴] 3 <= 4
// [1-4턴] 4 <= 4
// ---------------------------------
// [2-1턴] 1 <= ( 4 + 2 - 1) 5
// [2-2턴] 2 <= 5
// [2-3턴] 3 <= 5
// [2-4턴] 4 <= 5
// [2-5턴] 5 <= 5
if(input-row >= col) { // 공백 조건
// [1-1턴] (4 - 1) 3 >= 1(true) " " 출력
// [1-2턴] 3 >= 2(true) " " 출력
// [1-3턴] 3 >= 3(true) " " 출력
// [1-4턴] 3 >= 4(false) "*" 출력
// ---------------------------------------
// [2-1턴] (4 - 2) 2 >= 1(true) " " 출력
// [2-2턴] 2 >= 2(true) " " 출력
// [2-3턴] 2 >= 3(false) "*" 출력
// [2-4턴] 2 >= 4(false) "*" 출력
// [2-5턴] 2 >= 5(false) "*" 출력
System.out.print(" ");
} else { // * 조건
System.out.print("*");
}
}
System.out.println();
// [1-5턴] col이 5가 되면서 안쪽 for문 false로 줄바꿈하고 바깥 for문 다시 감
}
}
메소드 명 : public void practice6( ){ }
다음과 같은 실행 예제를 구현하세요.
ex) 정수 입력 : 5
*****
* *
* *
* *
*****
Public void practice6() {
Scanner sc = new Scanner(System.in);
System.out.println("정수 입력 : ");
int input = sc.nextInt();
for(int row = 1; row <= input; row++) {
for(int col = 1; col <= input; col++) {
// 첫 번째 / 마지막 행, 첫 번째, 마지막 열 * 출력
if(row == 1 || row == input || col == 1 || col == input) {
System.out.print("*");
} else { // 나머지는 공백 출력
System.out.print(" ");
}
}
System.out.println();
}
}