#for문 예제: 주어진 모양 출력하기
public class Prac06 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
package practice_for;
public class Prac06 {
public static void main(String[] args) {
//sol1
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <=i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
//sol2
for (int i = 1; i <= 5; i++) {
int blank=5-i;
int star=i;
for(int j=1;j<=blank;j++) {
System.out.print(" ");
}
for (int k = 1; k<=star; k++) {
System.out.print("*");
}
System.out.println(" ");
}
}
}
package practice_for;
public class Prac06 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j>=i; j--) {
System.out.print("*");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
package practice_for;
public class Prac06 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print("*");
}
for (int j = 4; j >=1; j--) {
System.out.print(" ");
}
System.out.print("\n");
}
}
}
package practice_for;
public class Prac06 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j<=i-1; j++) {
System.out.print(" ");
}
for (int k = 1; k<=6-i; k++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
java : 8일차 (0) | 2023.01.19 |
---|---|
java:7일차 (0) | 2023.01.19 |
java : 5일차 (0) | 2023.01.15 |
java:4일차 (0) | 2023.01.05 |
java:3일차 (0) | 2023.01.05 |
댓글 영역