Language/Java

[자바_복습] 중첩for문 활용_구구단/행렬/별찍기

simDev1234 2022. 3. 11. 19:34

구구단/행렬/별찍기 모두 원리는 비슷했다. 

결국 수학의 (x , y) 행렬이라 볼 수 있는데, 중첩 for문을 사용해서 구현이 가능하다.

------------------------------------------------------------------------------------------------------------------- 

 

1. 구구단 예제

 

[1] 구구단 2~9단을 출력하라_중첩 for문 활용

package practice;

public class dan99TestBasic {

	public static void main(String[] args) {
		for(int i = 2; i <= 9; i++) {
			System.out.println("--------["+i+"단 시작]--------");
			for(int j = 1; j <= 9; j++) {
				System.out.printf("%d x %d = %2d%n",i,j,i*j);
			}
		}
	}

}

>> 결과

--------[2단 시작]--------
2 x 1 =  2
2 x 2 =  4
2 x 3 =  6
2 x 4 =  8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
--------[3단 시작]--------
3 x 1 =  3
3 x 2 =  6
3 x 3 =  9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
--------[4단 시작]--------
4 x 1 =  4
4 x 2 =  8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
--------[5단 시작]--------
5 x 1 =  5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
--------[6단 시작]--------
6 x 1 =  6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
--------[7단 시작]--------
7 x 1 =  7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
--------[8단 시작]--------
8 x 1 =  8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
--------[9단 시작]--------
9 x 1 =  9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81

 

[1-2] 사용자가 입력한 x단의 구구단을 출력하라_do_while 활용

(단, 사용자는 여러 번 구구단을 입력하고 결과를 받을 수 있어야 한다.)

package practice;

import java.util.Scanner;

public class Dan99Test2 {

	public static void main(String[] args) {
				
		int dan = 0;
		String yn = "Y";
		Scanner scanner = new Scanner(System.in);
		
		do
		{
			System.out.print("단 입력(2~9단 중 하나) : ");
			dan = scanner.nextInt();
			
			if(dan >= 2 && dan <= 9) {
				for(int i = 1; i <=9; i++) {
					System.out.printf("%d x %d = %2d\n",dan,i,dan*i);
				}
				System.out.print("더 하시겠습니까? (y/n) :");
				yn = scanner.next();
			}else {
				System.out.println("2~9 중 하나를 입력해주세요.");
			}
		}while(yn.equals("Y") || yn.equals("y"));
			
		scanner.close();
	}

}

>> 결과

단 입력(2~9단 중 하나) : 3
3 x 1 =  3
3 x 2 =  6
3 x 3 =  9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
더 하시겠습니까? (y/n) :n

 

2. 행렬

 

[1] 아래와 같은 형태의 행렬을 출력하라.

>> 결과1

(1,1) (1,2) (1,3) (1,4) (1,5) 
(2,1) (2,2) (2,3) (2,4) (2,5) 
(3,1) (3,2) (3,3) (3,4) (3,5) 
(4,1) (4,2) (4,3) (4,4) (4,5) 
(5,1) (5,2) (5,3) (5,4) (5,5)

 

>> 소스코드

package practice;

public class MatrixTest {

	public static void main(String[] args) {
		//중첩for문_행렬 구하기
		int row = 5;
		int col = 5;
		for(int i = 1; i <= row; i++) {
			for(int j = 1; j <= col; j++) {
				System.out.printf("(%d,%d) ",i, j);
			}
			System.out.println();
		}
	}

}

 

[2] 아래와 같이 좌->우로 내려오는 대각선에 공백을 둔 행렬을 출력하라

>> 결과

( , ) (1,2) (1,3) (1,4) (1,5) 
(2,1) ( , ) (2,3) (2,4) (2,5) 
(3,1) (3,2) ( , ) (3,4) (3,5) 
(4,1) (4,2) (4,3) ( , ) (4,5) 
(5,1) (5,2) (5,3) (5,4) ( , )

>> 소스코드

package practice;

public class MatrixTest {

	public static void main(String[] args) {	
		//중첩for문+continue_행렬에서 i==j값 공백
		for(int i = 1; i <= row; i++) {
			for(int j = 1; j <= col; j++) {
				if(i == j) {
					System.out.printf("( , ) ",i, j);
					continue;
				} 
				System.out.printf("(%d,%d) ",i, j);
			}
			System.out.println();
		}
	}

}

 

[3] 아래와 같이 좌->우로 내려오는 대각선에 공백을 두고, 대각선의 우측은 제외하여 출력하라.

>> 결과

( , ) 
(2,1) ( , ) 
(3,1) (3,2) ( , ) 
(4,1) (4,2) (4,3) ( , ) 
(5,1) (5,2) (5,3) (5,4) ( , )

>> 소스코드

package practice;

public class MatrixTest {

	public static void main(String[] args) {
		//중첩for문+continue label_행렬에서 i==j값 공백, i<j면 미출력
		OUT_FOR : 
		for(int i = 1; i <= row; i++) {
			for(int j = 1; j <= col; j++) {
				if(i == j) {
					System.out.printf("( , ) ",i, j);
					continue;
				}else if(i < j) {
					System.out.println();
					continue OUT_FOR;
				}else {
					System.out.printf("(%d,%d) ",i, j);
				}
			}
			System.out.println();
		}
	}

}

 

3. 별찍기

 

 [1] 정사각형 별찍기

package practice;

public class StarTest1 {

	public static void main(String[] args) {
		int row = 5;
		int col = 5;
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}

}

>> 결과

*****
*****
*****
*****
*****

 

[2] 반피라미드 - 좌->우 대각선 하위만 출력

package practice;

public class StarTest2 {

	public static void main(String[] args) {
		int row = 5;
		int col = 5;
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				if(j <= i) {
					System.out.print("*");
				}
			}
			System.out.println();
		}
	}

}

>> 결과

*
**
***
****
*****

 

[3] 역 반피라미드 - 좌->우 대각선 상위만 출력

package practice;

public class StarTest3 {

	public static void main(String[] args) {
		int row = 5;
		int col = 5;
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				if(j >= i) {
					System.out.print("*");
				}else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}

}

>> 결과

*****
 ****
  ***
   **
    *

 

[4] 바람개비 - 좌->우, 우->좌 대각선 제외 출력 

package practice;

public class StarTest4 {

	public static void main(String[] args) {
		int row = 5;
		int col = 5;
		
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				if(i == j || 4-j==i) {
					System.out.print(" ");
				}else {
					System.out.print("*");
				}
			}
			System.out.println();
		}
	}

}

>> 결과

 *** 
* * *
** **
* * *
 ***

 

[4-2] 반피라미드 - 우->좌 대각선 하위만 출력 (그림에 이걸 빼먹었다..ㅎ)

package practice;

public class StarTest6 {

	public static void main(String[] args) {
		int row = 5;
		int col = 5;
		for(int i = 0; i < row; i++) {
			for(int j = col-1; j >= 0; j--) {
				if(j <= i) {
					System.out.print("*");
				}else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}

}

>> 결과

    *
   **
  ***
 ****
*****

 

[5] 마름모 출력하기

package practice;

public class StarTest5 {

	public static void main(String[] args) {
		int n = 5;
		int mid = n/2;
		
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(i <= mid) { //상단
					//가운데를 기준으로,
					//2-i <= j <= 2+i 일때에 출력
					if((j >= mid-i) && (j <= mid+i)) {
						System.out.print("*");
					}else {
						System.out.print(" ");
					}
				}else { //하단
					int k = (n-1)-i; //임시변수k 새로 만들어줘야함
					if((j >= mid-k) && (j <= mid+k)) {
						System.out.print("*");
					}else {
						System.out.print(" ");
					}
				}
			}
			System.out.println();
		}
	}

}

>> 결과

  *  
 *** 
*****
 *** 
  *