JAVA 문제풀이

반복문 실습문제.10

Rocomi 2024. 6. 20. 14:59

다음과 같은 실행 예제를 구현하세요.

정수 입력 : 4

*

**

***

****

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.kh.practice.chap02.loop;
 
import java.util.Scanner;
 
public class LoopPractice {
 
    public void practice13() {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("정수 입력 : ");
        int num = sc.nextInt();
        
        for(int i = 0; i <= num; i++) {
            for(int j = 1; j <= i; j++) {
                    System.out.print("*");
            if( i == j) {
                System.out.print("\n");
            }
            }
            
        }
    }
    
}
cs

 

정수 입력 : 4

****

***

**

*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.kh.practice.chap02.loop;
 
import java.util.Scanner;
 
public class LoopPractice {
 
    public void practice14() {        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("정수 입력 : ");
        int num = sc.nextInt();
        
        for(int i = num; i > 0; i--) {
            for(int j = i; j > 0; j--) {
                    System.out.print("*");
            if( j == 1) {
                System.out.print("\n");
            }
            }
            
        }
    }
    
}
cs