Contents
중첩 반복문중첩 반복문
반복문 안에 다른 반복문이 실행되는 것
주로 구구단 만들기에 쓰인다
public class NestedLoop { public static void main(String[] args) { for (int i = 0; i < 6; i++) { for (int j = 0; j < 10; j++) System.out.print("★"); System.out.println(""); } } }

위의 예제를 이해하면 쉽다.
구구단 만들기
public class GugudanEx01 { public static void main(String[] args) { for (int j = 2; j <= 9; j++) { for (int i = 1; i <= 9; i++) System.out.printf("%d X %d = %d\n",j, i, j * i); System.out.println("=========="); } } }


구구단 가로로
세로로 만드니 너무 길어서 가로로 펼쳐보자
public class GugudanEx03 { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j = 2; j <= 9; j++) System.out.printf("%d X %d = %d\t",j, i, j * i); System.out.println(); } } }
for문의 위치를 바꾸면 다음과 같이 출력된다.

\n
때문인데 \t
으로 바꾸어 ‘tap'을 입력한다.
그리고 가로 한 줄이 끝나면 System.
out
.println()
을 출력하여 다음줄로 넘어간다.
Share article