728x90
배열을 스윽 그려보자
public class K6 {
public static void main(String[] args) {
for(int i=5; i>0; i--) { //i별갯수
for(int j=0; j<i; j++) {//처음 별개수가 5개 이므로 j가 4가 될때까지 4개찍음
System.out.print("*");
}
System.out.println();
}
}
}
줄 갯수를 입력받아 출력해보자.
import java.util.Scanner;
public class G {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//변수 선언
int a;
//값 받기
System.out.print("몇줄? >");
a = scan.nextInt();
//생성
for(int i=a; i>0; i--) { //i별갯수
for(int j=0; j<i; j++) {//처음 별개수가 5개 이므로 j가 4가 될때까지 4개찍음
System.out.print("*");
}
System.out.println();
scan.close();
}
}
}
이렇게 된다
역방향도 스윽 그려보자
public class K5 {
public static void main(String[] args) {
for (int i=0; i<5; i++) {//줄생성
for (int j=0; j<i; j++) {
System.out.print(" ");
}
for (int j=4; j>=i; j--) {
System.out.print("*");
}
System.out.println();
}
}
}
모양이 이쁘게 그려졌다.
자 이제 줄 갯수를 입력받아 출력해보자.
import java.util.Scanner;
public class F {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//변수 선언
int a;
//값 받기
System.out.print("몇줄? >");
a = scan.nextInt();
//생성
for (int i=0; i<a; i++) {//줄생성
for (int j=0; j<i; j++) {
System.out.print(" ");
}
for (int j=a-1; j>=i; j--) {
System.out.print("*");
}
System.out.println();
scan.close();
}
}
}
줄수를 입력하니 마치 마법처럼 역 삼각형이 그려진다
우와아! 아주 만족스럽다.
728x90
'프로그래밍 > JAVA' 카테고리의 다른 글
자바 구구단 배열로 출력하기 (0) | 2022.12.05 |
---|---|
for-each 문 (0) | 2022.11.30 |
자바 사각형 플레이 버튼 그리기 (0) | 2022.11.29 |
자바 역방향 피라미드 별찍기로 그리기 (0) | 2022.11.29 |
자바 피라미드 별찍기로 그리기 (0) | 2022.11.29 |