본문 바로가기
문제 풀이/CodeUp

CodeUp_1357 : 삼각형 출력하기 4

by 윤지(●'◡'●) 2021. 4. 23.
반응형


문제내용

n이 입력되면 다음 삼각형을 출력하시오.

예) n = 4
*
**
***
****
***
**
*

 


자바코드

import java.util.Scanner;

public class CodeUp_1357 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();


		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
		
		for (int i = 1; i < n; i++) {	
			for (int j = n; j > i; j--) {
				System.out.print("*");
			}
			System.out.println();
		}
	}

}

 

 

 

 

 

코드풀이

 

 

import java.util.Scanner;

public class CodeUp_1357 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in); // Scanner 객체 생성
		
		int n = sc.nextInt(); // 입력한 정수를 변수 n에 저장


		for (int i = 0; i < n; i++) { // n 만큼 반복
			for (int j = 0; j <= i; j++) { // i보다 작거나 같은 동안 반복, ++
				System.out.print("*"); 
			}
			System.out.println();
		}
		
		for (int i = 1; i < n; i++) {	
			for (int j = n; j > i; j--) { // i보다 작은동안 반복, --
				System.out.print("*");
			}
			System.out.println();
		}
	}

}

 

 

 

출력내용

 

4
*
**
***
****
***
**
*
3
*
**
***
**
*
반응형

댓글