문제: 구구단 출력
중첩 for문을 사용해서 구구단을 완성해라.
출력 형태
1 * 1 = 1
1 * 2 = 2
...
9 * 9 = 81
문제 풀이
package loop;
public class LoopEx7 {
public static void main(String[] args) {
for (int i =1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
System.out.println(i + " * " + j + " = " + i*j);
}
}
}
}
문제: 피라미드 출력
int rows 를 선언해라. 이 수만큼 다음과 같은 피라미드를 출력하면 된다. 참고: println() 은 출력후 다음 라인으로 넘어간다. 라인을 넘기지 않고 출력하려면 print() 를 사용하면 된다. 예) System.out.print("*")
출력 형태
//rows = 2
*
**
//rows = 5
*
**
***
****
*****
문제 풀이
package loop;
public class LoopEx8 {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++){
System.out.println("");
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
}
}
}
'JAVA' 카테고리의 다른 글
[JAVA] Section7) 훈련 (0) | 2024.03.12 |
---|---|
[JAVA] Section6) 스코프, 형변환 (0) | 2024.03.12 |
[JAVA] Section5) Problems1 (0) | 2024.03.09 |
[JAVA] Section5) 반복문 (0) | 2024.03.09 |
[JAVA] Section4) Problems2 (0) | 2024.03.07 |