Data Scientist 옌

매일 발전하는 IT문제해결사

국비지원교육 (22.01-22.07)/강의노트

22-02-11(금) 012일차 [Java] while문, do while문, 2차원 배열

옌炎 2022. 4. 16. 14:22
728x90

수업내용


1교시 (09:30-10:20)

  • 어제 내용 복습
for 문
---------------
1. 반복 수행하는 넘이다. loop 
2. from to : 어디부터 어디까지 반복 수행을 할 수 있다. 
3.	for (초기화식; 조건식; 증감식) {
		초기화식에서 선언한 변수를 사용할 수 있다.

		==============================================
		초기화식에서 선언한 변수는 for 블럭 밖으로 나갈 수 없다.
		==============================================

		========================================
		for 블럭 위쪽에서 선언한 변수에 
		for 소괄호에서 변수를 초기화 하면
		for 블럭 밖에서도 사용할 수 가 있다. 
		int i=0;
		for (i=1; 조건식; 증감식) {
			i 사용가능;
		}
		i 사용가능;
		========================================
	}
4. 조건식이 true 일 때만 for 블럭의 수행문이 수행된다. 
5. 증감식은 : 변수++, 변수 += 2 ... 
6. 증감은 : ++, -- 다 가능하다.

2교시 (10:30-11:20)

  • Exam_For_7
package a.b.c.ch1;

public class Exam_For_7 {

	public static void main(String args[]) {

		// int iArr[] = new iArr[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
		int iArr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
		System.out.println("iArr 참조변수 주소값 >>> : " + iArr);

		iArr = null;
		
		/*
		int iArrLen = iArr.length;
		System.out.println("iArr.length 배열의 길이 >>> : " + iArr.length);

		if (iArr > 0) {
			for (int i=0; i < iArrLen; i++) {
				System.out.println("iArr[" + i + "] >>> : " + iArr[i]);
			}
		}
		*/

		int iArrlen = 0;

		if (iArr !=null && iArr.length > 0) {
		
			iArrlen = iArr.length;
			System.out.println("iArr,length 배열의 길이 >>> : " + iArr.length);

			for (int i=0; i < iArrlen; i++) {
				System.out.println("iArr[" + i + "] >>> : " + iArr[i]);
			}
		} else {
			System.out.println("iArr 참조변수에 null이거나 데이터가 없어요 >>> : " + iArr);
		}
	
	} // end of main()

} // end of Exam_For_7 class
  • Exam_For_7_1
package a.b.c.ch1;

// length() : 랭스 함수 : String 클래스의 문자열 길이를 구하는 함수 
// length : 랭스 필드 : 배열의 길이

public class Exam_For_7_1 {

	// forTest 함수를 리턴값 없이 선언한다.
	// 매개변수는 int 형 배열이다. 
	// int  형 배열의 참조변수는 iArr_forTest 이다. 
	// forTest 함수를 호출할 때는 인수(아규먼트)를  int 형 배열의 참조변수를 넘겨야 한다.
	// 단 변수명은 동일하지 않아도 된다.
	/*
		Exam_For_7_1 ef7_1 = new Exam_For_7_1();
		ef7_1.forTest(iArr_main);
	
		int iArr_forTest[] = iArr_main;	
		public void forTest(int iArr_forTest[]){}
	*/
	public void forTest(int iArr_forTest[]) {
		System.out.println("Exam_For_7_1.forTest() 함수 시작 >>> : ");
		System.out.println("Exam_For_7_1.forTest() 함수 iArr_forTest >>> : " 
							+ iArr_forTest);

		// 지역변수 : 배열의 길이를 체크하기 위해서 
		int iArrLen = 0;

		// forTest 의 매개변수 넘어온 iArr_forTest 배열 참조변수에 데이터가 있는지 없는지 체크하는 것
		// 배열은 객체이기 때문에 null(iArr_forTest !=null) 
		// 및 길이(iArr_forTest.length > 0)를 체크한다.
		if (iArr_forTest !=null && iArr_forTest.length > 0){

			// 배열의 길이 체크 
			iArrLen = iArr_forTest.length;
			System.out.println("iArrLen 배열의 길이 >>> : " + iArrLen);

			// for 문을 이용해서 배열의 길이 만큼 배열의 데이터르 콘솔에 출력하기 
			for (int i=0; i < iArrLen; i++ ){
				System.out.println("iArr_forTest[" + i + "] >>> : " + iArr_forTest[i]);
			}

		}else{
			System.out.println("iArr_forTest 참조변수에 null 이거나 데이터가 없어요 >>> : " 
								+ iArr_forTest);
		}
	}


	public static void main(String args[]) {
	
		// int iArr[] = new iArr[]{1, 2, 3, 4, 5, 6, 7, 8, 9};

		// 지역 변수 
		// int 기초자료형 인데 배열로 인스턴스 해서 객체이면서 참조변수를 사용한다.
		// iArr_main : 참조변수 
		// int 형 배열을 iArr_main 참조변수 1차원 배열을 선언 과 동시 초기화했다.
		int iArr_main[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
		System.out.println("iArr_main 배열의 참조변수 주소값 >>> : " + iArr_main);
		
		// 사용자 정의 Exam_For_7_1 클래스를 사용하기 위해 인스턴스 했다(메모리에 올렸다).
		// 참조 변수는 ef7_1 이다. 
		Exam_For_7_1 ef7_1 = new Exam_For_7_1();
		System.out.println("ef7_1 참조변수 주소값 >>> : " + ef7_1);
		
		// 참조 변수 ef7_1 를 이용해서 Exam_For_7_1 클래스의 자원 중 
		// forTest() 함수를 호출하고 함수의 매개변수에 인수 iArr_main 배열 참조변수를 넘겨준다.
		// iArr_main : 인수, 아규먼트, argument
		// 메모리에 올라가 forTest 함수가 인터트리트 방식으로 수행된다 .
		ef7_1.forTest(iArr_main);
	}
}

3교시 (11:30-12:20)

  • [교재 107p] 반복문 - while
package a.b.c.ch1;

public class Exam_while {

	public static void main(String args[]) {

		boolean bool = false;

		while (bool) {
			System.out.println(">>>");
		}
	
	}  // end of main()

} // end of Exam_while class
  • [교재 109p] 예제
package a.b.c.ch1;

public class Exam_while {

	public static void main(String args[]) {

		boolean bool = false;

		while (bool) {
			System.out.println(">>>");
		}

		int num = 1;
		int sum = 0;
		int count = 0;

		while (num <= 10){
			count++;
			sum += num;
			num++;

		}
			System.out.println("1부터 10까지의 연산합은 " + sum + " 입니다.");	
			System.out.println(count + " 연산했습니다.");

	}  // end of main()

} // end of Exam_while class

4교시 (12:30-13:20)

  • Exam_while
package a.b.c.ch1;

public class Exam_while {

	public static void main(String args[]) {

		boolean bool = false;

		while (bool) {
			System.out.println(">>>");
		}

		// 지역변수
		int num = 1;
		int sum = 0;
		int count = 0;

		while (num <= 10){
			count++;
			sum += num;
			System.out.println(count + "번째 연산 >>> : " + sum + " ");
			if (sum > 20){				
				break;
			}

			num++;
		}
			System.out.println("1부터 10까지의 연산합은 " + sum + " 입니다.");	
			System.out.println(count + " 연산했습니다.");

	}  // end of main()

} // end of Exam_while class
  • do while
package a.b.c.ch1;

public class Exam_while_1 {

	public static void main(String args[]) {
		
		// 지역변수 
		int num = 1;
		int sum = 0;
		
		// do는 무조건 실행
		// while은 true일 때만 실행
		do{
			sum += num;
			num++;
		}
		while (num <= 10);

		System.out.println("1부터 10 까지의 합은 " + sum + " 입니다.");
	}
}

5교시 (14:30-15:20)

  • 2차원 배열
package a.b.c.ch1;

public class Exam_For_8 {

	public static void main(String args[]) {

		for (int i=0; i < 3; i++){
			System.out.print(">>> : " + i);

			for (int j=0; j < 3; j++){
				System.out.println("<<<>>> : " + j);
			}
		}
	
		// int iA[] = new iA[]{1, 2, 3};
		int iA[] = {1, 2, 3};
		for (int i=0; i < iA.length; i++){
			System.out.println("" + iA[i]);
		}

		// int iA2[][] = new iA2[][]{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
		int iA2[][] = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
		for (int i=0; i < iA2.length; i++){
			System.out.println("" + iA2[i]);
		}

	} // end of main()

} // end of Exam_For_8 class
  • 2차원 배열과 이중 포문
package a.b.c.ch1;

public class Exam_For_8 {

	public static void main(String args[]) {

		for (int i=0; i < 3; i++){
			System.out.print(">>> : " + i);

			for (int j=0; j < 3; j++){
				System.out.println("<<<>>> : " + j);
			}
		}

		for (int i=0; i < 3; i++ ){			
			for (int j=0; j < 3; j++){
				System.out.print("(" + i + "," + j + ") ");
			}
			System.out.println();
		}
		/*
		 i j   i j   i j
		(0,0) (0,1) (0,2)
		(1,0) (1,1) (1,2)
		(2,0) (2,1) (2,2)		
		*/
	
		for (int i=0; i < 3; i++){
			for (int j=0; j < 3; j++){
				System.out.print("*");
			}
			System.out.println();
		}
		/*
		***
		***
		***
		*/

		// int iA[] = new iA[]{1, 2, 3};
		int iA[] = {1, 2, 3};
		for (int i=0; i < iA.length; i++){
			System.out.println("" + iA[i]);
		}

		// int iA2[][] = new iA2[][]{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
		int iA2[][] = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
		for (int i=0; i < iA2.length; i++){
			System.out.println("" + iA2[i]);
		}

	} // end of main()

} // end of Exam_For_8 class

6교시 (15:30-16:20)

  • for문 별찍기 연습
package a.b.c.ch1;

public class Exam_For_8 {

	public static void main(String args[]) {

		for (int i=0; i < 3; i++){
			System.out.print(">>> : " + i);

			for (int j=0; j < 3; j++){
				System.out.println("<<<>>> : " + j);
			}
		}

		for (int i=0; i < 3; i++ ){			
			for (int j=0; j < 3; j++){
				System.out.print("(" + i + "," + j + ") ");
			}
			System.out.println();
		}
		/*
			 i j   i j   i j
			(0,0) (0,1) (0,2)
			(1,0) (1,1) (1,2)
			(2,0) (2,1) (2,2)		
		*/
	
		for (int i=0; i < 3; i++){
			for (int j=0; j < 3; j++){
				System.out.print("*");
			}
			System.out.println();
		}
		/*
			***
			***
			***
		*/

		for (int i=0; i < 3; i++){
			for (int j=0; j < i; j++){
				System.out.print("*");
			}
			System.out.println("");
		}
		System.out.println("");

		/*
			*
			**
		*/

		for (int i=0; i < 3; i++ ){			
			for (int j=3; j > i; j--){
				System.out.print("*");
			}
			System.out.println();
		}
		/*
			***
			**
			*
		*/

		// int iA[] = new iA[]{1, 2, 3};
		int iA[] = {1, 2, 3};
		for (int i=0; i < iA.length; i++){
			System.out.println("" + iA[i]);
		}

		// int iA2[][] = new iA2[][]{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
		int iA2[][] = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
		for (int i=0; i < iA2.length; i++){
			System.out.println("" + iA2[i]);
		}

		for (int i=0; i < iA2.length; i++){
			// System.out.println("" + iA2[i]);
			for (int j=0; j < iA2[i].length; j++){
				System.out.print(iA2[i][j]);
			}
			System.out.println();
		/*
			123
			123
			123
		*/
		}

	} // end of main()

} // end of Exam_For_8 class

7교시 (16:30-17:20)

  • 구구단 출력하기
package a.b.c.ch1;

public class Exam_For_9 {

	public static void main(String args[]) {

		for (int j=1; j <= 9; j++){
			System.out.println();

			for (int i=2; i <= 5; i++){
				/*		
				java.util.PrintStream
				public PrintStream format(String format,
										Object... args)

				java.lang.String
				public static String format(String format,
											Object... args)
				*/
				System.out.format("%d x %d = %2d  ", i, j, i * j);
			}
		}		
		System.out.println();

		
		for (int j=1; j <= 9; j++){
			System.out.println();

			for (int i=6; i <= 9; i++){
				/*
				public PrintStream printf(String format, Object... args)
				String format : "%d x %d = %2d  "
				Object... args : i, j, i * j
				*/
				System.out.printf("%d x %d = %2d  ", i, j, i * j);
			}
		}		
	}
}
/*
	서식 지정자 format specifier
		%d		정수형 출력
		%s		문자형 출력
		%f		실수형 출력
		%o		8진수 출력
		%x, %X	16진수 출력
		%e, %E	지수 출력
		%c		문자 출력
		%n		줄 바꿈
		%b		boolean 출력
*/
  • 내가 짠 코드
package a.b.c.ch1;

/*
	콘솔에 a b c ... 으로 출력하시오
*/

public class Exam_For_10_test {

	public static void main(String args[]) {
	
		String sArr[] = {"abc", "def", "ghi"};

		for (int i=0; i < sArr.length; i++){

			for (int j=0; j < sArr[i].length(); j++){

				System.out.print(sArr[i].charAt(j) + " ");
			}
		}

	} // end of main() 

} // end of Exam_For_10_test class

8교시 (17:30-18:30)

  • 선생님이 주신 코드
package a.b.c.ch1;

/*
String sArr = {"abc", "def", "ghi"};
콘솔에 a b c ... 으로 출력하시오
*/


public class Exam_For_10 {

	public void stringFor(String sArr[]){
		System.out.println("Exam_For_10.stringFor 함수 진입 >>> : ");
		System.out.println("stringFor ::: sArr 참조변수 주소값 >>> : " + sArr);

		if (sArr !=null && sArr.length > 0){

			for (int i=0; i < sArr.length; i++){
				System.out.println(">>> : " + sArr[i]);
				for (int j=0; j < sArr[i].length(); j++ ){
					System.out.println("<<<>>> : " + sArr[i].charAt(j));
				}
			}
		}
		
	}

	public static void main(String args[]) {

		// String sArr[] = new String[]{"abc", "def", "ghi"};
		String sArr[] = {"abc", "def", "ghi"};
		System.out.println("sArr.length >>> : " + sArr.length);
		System.out.println("sArr[0] >>> : " + sArr[0]);
		System.out.println("sArr[0] <<<>>> : " + sArr[0].charAt(0));
		System.out.println("sArr[0] <<<>>> : " + sArr[0].charAt(1));
		System.out.println("sArr[0] <<<>>> : " + sArr[0].charAt(2));
		System.out.println("sArr[0] >>> : " + sArr[1]);
		System.out.println("sArr[1] <<<>>> : " + sArr[1].charAt(0));
		System.out.println("sArr[1] <<<>>> : " + sArr[1].charAt(1));
		System.out.println("sArr[1] <<<>>> : " + sArr[1].charAt(2));
		System.out.println("sArr[0] >>> : " + sArr[2]);
		System.out.println("sArr[2] <<<>>> : " + sArr[2].charAt(0));
		System.out.println("sArr[2] <<<>>> : " + sArr[2].charAt(1));
		System.out.println("sArr[2] <<<>>> : " + sArr[2].charAt(2));

		System.out.println("main ::: sArr 참조변수 주소값 >>> : " + sArr);
		new Exam_For_10().stringFor(sArr);
	}
}

Notes


 

728x90