반응형
숫자 입력.
5
*
**
***
****
*****
---------2----------
*****
****
***
**
*
---------3----------
*
**
***
****
*****
---------4----------
*****
****
***
**
*
더보기
import java.util.Scanner;
public class Star1_4 {
public static void main(String[] args) {
// 1
Scanner scanner = new Scanner(System.in);
System.out.println("숫자 입력.");
int a = scanner.nextInt();
for (int i = 0; i < a; i++) {
String star = new String();
for (int j = 0; j <= i; j++) {
star += "*";
}
System.out.println(star);
}
System.out.println();
System.out.println("---------2----------");
System.out.println();
// 2
for (int i = 0; i < a; i++) {
String star = new String();
for (int j = a; j > i; j--) {
star += "*";
}
System.out.println(star);
}
// 3
System.out.println();
System.out.println("---------3----------");
System.out.println();
for (int i = 0; i < a; i++) {
String star = new String();
for (int j = (a - 1); j > i; j--) {
star += " ";
}
for (int j = 0; j <= i; j++) {
star += "*";
}
System.out.println(star);
}
System.out.println();
System.out.println("---------4----------");
System.out.println();
for (int i = 0; i < a; i++) {
String star = new String();
for (int j = 1; j <= i; j++) {
star += " ";
}
for (int j = a; j > i; j--) {
star += "*";
}
System.out.println(star);
}
scanner.close();
}
}
반응형
'Dev > Java' 카테고리의 다른 글
Java - 클래스 ( Class ) (0) | 2020.08.14 |
---|---|
Java - 예제 ) 별찍기 02 (0) | 2020.08.12 |
Java 예제 (0) | 2020.08.11 |
Java - 무한반복문 ( Infinite Loop ) (0) | 2020.08.11 |
Java - 반복문 ( While Loop ) (0) | 2020.08.11 |