Data Scientist 옌

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

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

22-02-10(목) 011일차 [Java] for문

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

수업내용


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

  • 어제 배운 내용 복습
  • import 키워드 사용방법 사용자 정의 클래스 만들 때 package 키워드하고 class 키워드 중간에 기술(사용)하면 된다. 사용자 정의 클래스에서 자원을 사용하려고 할 때 사용하는 키워드이다. C:\\Program Files\\Java\\jdk1.8.0._202\\jre\\lib\\rt.jar C:\\00.KOSMO108\\10.JExam\\ch1\\Exam_FlowControl_5.java import java.lang.String; import java.lang.Integer; import java.lang.System; import java.utill.Scanner; import java.utill.Date; import java.utill.Calendar; 문자 숫자 --> 숫자 parse "1" + "2" = "12" -----------> 1 + 2 = 3; 숫자 --> 문자 Integer.toString() String.valueOf() 연산자 1. == : 상등 연산자(등가 연산자) : 숫자(문자 포함 char : 양의 정수 16비트) 비교할 때만 사용한다. 2. 자바 문자열 비교 : equals() 함수로 한다. 단, 자바스크립트에서는 문자열,숫자 == 연산자로 한다. 3. 논리 연산 : short Circuit 4. 관계 연산 5. 증감 연산자 (전위, 후위) int a= 1; a++; --> int b = a++; int b = a = a + 1; 6. 삼항 연산자 조건식 ? 참 : 거짓; 7. 문자열 검증 String str; str = null; str = ""; ---> str.length() = 0; if (str != null && str.length() > 0) { }

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

  • [교재 112p] for문
package a.b.c.ch1;

// 교재 112p
// 어떤 조건부터 어떤 조건까지 반복 수행
// from      to        loop

public class Exam_For {

	public static void main(String args[]) {

		// 자바에서 for
		// for 키워드
		// (초기화식; 조건식; 증감식) 소괄호
			// 초기화식 : 데이터타입 선언 변수 선언 = 값 초기화; int i = 0;
			// 조건식 : boolean = 1 < 5;
			// 증감식 : 초기화 식에 선언한 변수를 이용해서 증감식을 쓴다. i++;
			// 증감식 연산자는 전위, 후위, ++, --, 배수 무엇이든 사용가능하다.
		// {반복수행할 표현식;} 중괄호

		// 자바에서는 for 키워드를 자바 버추얼 머신이 보면
		// for 키워드 뒤에 ()가 있으면 무조건 반복 수행한다.
		
		/*
		for (; ; ) {
			System.out.println(">>>>");
		}
		*/

		/* CASE01
		for (int i=0; ; ) {
			System.out.println("i >>> : " + i);
		}
		*/

		/* CASE02
		int i=0;
		for (; ; ) {
			System.out.println("i >>> : " + i);
		}
		*/

		// CASE03
		int i;
		for (i=0; ; ) {
			System.out.println("i >>> " + i);
		}

	} // end of main()

} // end of Exam_For class

 

package a.b.c.ch1;

// 교재 112p
// 어떤 조건부터 어떤 조건까지 반복 수행
// from      to        loop

public class Exam_For {

	public static void main(String args[]) {

		// 자바에서 for
		// for 키워드
		// (초기화식; 조건식; 증감식) 소괄호
			// 초기화식 : 데이터타입 선언 변수 선언 = 값 초기화; int i = 0;
			// 조건식 : boolean = 1 < 5;
			// 증감식 : 초기화 식에 선언한 변수를 이용해서 증감식을 쓴다. i++;
			// 증감식 연산자는 전위, 후위, ++, --, 배수 무엇이든 사용가능하다.
		// {반복수행할 표현식;} 중괄호

		// 자바에서는 for 키워드를 자바 버추얼 머신이 보면
		// for 키워드 뒤에 ()가 있으면 무조건 반복 수행한다.
		
		/*
		for (; ; ) {
			System.out.println(">>>>");
		}
		*/

		/* CASE01
		for (int i=0; ; ) {
			System.out.println("i >>> : " + i);
		}
		*/

		/* CASE02
		int i=0;
		for (; ; ) {
			System.out.println("i >>> : " + i);
		}
		*/

		// CASE03
		int i;
		for (i=0; ; ) {
			System.out.println("i >>> " + i);
		}


	} // end of main()

} // end of Exam_For class

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

  • for문 예제
    package a.b.c.ch1;
    
    // 교재 112p
    // 어떤 조건부터 어떤 조건까지 반복 수행
    // from      to        loop
    
    public class Exam_For {
    
    	public static void main(String args[]) {
    
    		// 자바에서 for
    		// for 키워드
    		// (초기화식; 조건식; 증감식) 소괄호
    			// 초기화식 : 데이터타입 선언 변수 선언 = 값 초기화; int i = 0;
    			// 조건식 : boolean = 1 < 5;
    			// 증감식 : 초기화 식에 선언한 변수를 이용해서 증감식을 쓴다. i++;
    			// 증감식 연산자는 전위, 후위, ++, --, 배수 무엇이든 사용가능하다.
    		// {반복수행할 표현식;} 중괄호
    
    		// 자바에서는 for 키워드를 자바 버추얼 머신이 보면
    		// for 키워드 뒤에 ()가 있으면 무조건 반복 수행한다.
    		
    		/*
    		for (; ; ) {
    			System.out.println(">>>>");
    		}
    		*/
    
    		/* CASE01
    		for (int i=0; ; ) {
    			System.out.println("i >>> : " + i);
    		}
    		*/
    
    		/* CASE02
    		int i=0;
    		for (; ; ) {
    			System.out.println("i >>> : " + i);
    		}
    		*/
    
    		/* CASE03
    		int i;
    		for (i=0; ; ) {
    			System.out.println("i >>> " + i);
    		}
    		*/
    
    		/* CASE04
    		int i;
    		for (i=0; i < 3; i++) {
    			System.out.println("(" + i + " >>> : " + (i < 3));		
    			System.out.println("for {} 블록 내부 ::: i >>> : " + i);
    		}
    		*/
    
    		/* CASE05
    		int i;
    		for (i=0; i < 3; i++) {
    			System.out.println("(" + i + " < 3) >>> " + (i < 3));			
    			System.out.println("for {} 블록 내부 ::: i >>> :" + i);
    		}
    		*/
    
    		/* CASE06
    		int i;
    		for (i=0; i < 3; i++) {
    			System.out.println("C" + i + " < 3) >>> " + (i < 3));			
    			System.out.println("for {} 블록 내부 ::: i >>> : " + i);
    		}
    
    		System.out.println("for {} 블록 외부 ::: i >>> : " + i);
    		*/
    
    		for (int i = 0; i < 3; i++) {
    			System.out.println("(" + i + " < 3) >>> : " + (i < 3));		
    			System.out.println("for {} 블록 내부 ::: i >>> : " + i);
    		}
    
    		// for 블록 내부에서 i가 선언되어 for 블록 외부로는 i를 사용할 수 없다
    		// System.out.println("for {} 블록 외부 ::: i >>> : " + i);
    
    	} // end of main()
    
    } // end of Exam_For class
    ```
  • 위 for 문 함수화
package a.b.c.ch1;

public class Exam_For_1 {

	void for_1() {
		System.out.println("Exam_For_1.for_1() 함수 블록 시작");
		for (int i=0; ; ) {
			System.out.println("i >>> : " + i);
		}
	}

	void for_2() {
		System.out.println("Exam_For_1.for_2() 함수 블록 시작");
		int i = 0;
		for (; ; ) {
			System.out.println("i >>> : " + i);
		}
	}

	void for_3() {
		System.out.println("Exam_For_1.for_3() 함수 블록 시작");
		int i;
		for (i=0; ; ) {
			System.out.println("i >>> : " + i);
		}
	}

	void for_4() {
		System.out.println("Exam_For_1.for_4() 함수 블록 시작");
		int i;
		for (i=0; i <3; ) {
			System.out.println("(" + i + " < 3) >>> : " + (i < 3));			
			System.out.println("for {} 블록 내부 ::: i >>> : " + i);
		}
	}

	void for_5() {
		System.out.println("Exam_For_1.for_5() 함수 블록 시작");
		int i;
		for (i=0; i<3; i++) {
			System.out.println("(" + i + " < 3) >>> : " + (i < 3));		
			System.out.println("for {} 블록 내부 ::: i >>> : " + i);
		}
	}

	void for_6() {
		System.out.println("Exam_For_1.for_6() 함수 블록 시작");
		int i;
		for (i=0; i < 3; i++) {
			System.out.println("(" + i + " < 3) >>> :" + (i < 3));		
			System.out.println("for {} 블록 내부 ::: i >>> :" + i);
		}
		System.out.println("for {} 블록 외부 ::: i >>> : " + i);
	}

	void for_7() {
		System.out.println("Exam_For_1.for_7() 함수 블록 시작");
		for (int i=0; i<3; i++) {
			System.out.println("(" + i + " < 3) >>> : " + (i < 3));			
			System.out.println("for {} 블록 내부 ::: i >>> : " + i);
		}
		// System.out.println("for {} 블록 외부 ::: i >>> : " + i);
	}

	public static void main(String args[]) {
	
		// 명령행 인수 길이 체크하기
		if (args.length == 1){

			String sVal = args[0];

			if (sVal !=null && sVal.length() > 0) {
			
				if ("1".equals(sVal)) {
					System.out.println("함수 실행 번호 1 선택 >>> : ");
					System.out.println("Exam_for_1() 클래스를 인스턴스해서(메모리에 올려서)");
					System.out.println("for_1() 함수 호출 >>> : \n");
					new Exam_For_1().for_1();
				}
				
				if ("2".equals(sVal)) {
					System.out.println("함수 실행 번호 2 선택 >>> : ");
					System.out.println("Exam_for_2() 클래스를 인스턴스해서(메모리에 올려서)");
					System.out.println("for_2() 함수 호출 >>> : \n");
					new Exam_For_1().for_2();
				}

				if ("3".equals(sVal)) {
					System.out.println("함수 실행 번호 3 선택 >>> : ");
					System.out.println("Exam_for_3() 클래스를 인스턴스해서(메모리에 올려서)");
					System.out.println("for_3() 함수 호출 >>> : \n");
					new Exam_For_1().for_3();
				}

				if ("4".equals(sVal)) {
					System.out.println("함수 실행 번호 4 선택 >>> : ");
					System.out.println("Exam_for_4() 클래스를 인스턴스해서(메모리에 올려서)");
					System.out.println("for_4() 함수 호출 >>> : \n");
					new Exam_For_1().for_4();
				}

				if ("5".equals(sVal)) {
					System.out.println("함수 실행 번호 5 선택 >>> : ");
					System.out.println("Exam_for_5() 클래스를 인스턴스해서(메모리에 올려서)");
					System.out.println("for_5() 함수 호출 >>> : \n");
					new Exam_For_1().for_5();
				}

				if ("6".equals(sVal)) {
					System.out.println("함수 실행 번호 6 선택 >>> : ");
					System.out.println("Exam_for_6() 클래스를 인스턴스해서(메모리에 올려서)");
					System.out.println("for_6() 함수 호출 >>> : \n");
					new Exam_For_1().for_6();
				}

				if ("7".equals(sVal)) {
					System.out.println("함수 실행 번호 7 선택 >>> : ");
					System.out.println("Exam_for_7() 클래스를 인스턴스해서(메모리에 올려서)");
					System.out.println("for_7() 함수 호출 >>> : \n");
					new Exam_For_1().for_7();
				}	
			} else {
				System.out.println("sVal에 데이터가 없습니다. >>> : ");
			}
		} else {
			System.out.println("함수 실행 번호를 입력하시오 >>> : ");
		} 

	} // end of main()

} // end of Exam_For_1 class

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

  • for문의 변수 조절
package a.b.c.ch1;

public class Exam_For_2 {

	public static void main(String args[]) {
	
	/*
		자바에서 for문 초기화식에서 항상 초기화 값을 0으로 하는데
		그 이유는 for문이 대부분 배열 데이터를 처리하는데 사용되기 때문이다.
		배열의 인덱스는 0부터 시작한다.
	*/

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

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

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

		for (int i=1; (i-1) < 3; i++){
			System.out.println("i >>> : " + i);			
		}	

	} // end of main()

} // end of Exam_For_2 class
  • 예제 Exam_For_3
package a.b.c.ch1;

public class Exam_For_3 {

	public static void main(String args[]){
	
		for (int i=0; i <= 10; i += 2) {
			System.out.print(i + " ");
		}
		System.out.println();

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

		// 감소가 더 속도 빠름
		for (int i = 10; i >= 0; i--) {
			System.out.print(i + " ");
		}

	} // end of main()

} // end of Exam_For_3 class

/*
C:\00.KOSMO108\10.JExam\ch1>javac -d . Exam_For_3.java && java a.b.c.ch1.Exam_For_3
0 2 4 6 8 10
0 3 6 9
10 9 8 7 6 5 4 3 2 1 0
*/

 


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

  • Exam_For_4
package a.b.c.ch1;

public class Exam_For_4 {

	public static void main(String args[]) {
		
		/*
			1. 지역 변수, 초기화해서 사용해야 한다.
			2. 참조 변수, 주소값을 갖는다(생략).
				// public static int identityHashCode(Object x)
			3. rt.jar : java.lang.String : import하지 않아도 사용 가능
			4. 문자열을 다루는 빌트인 클래스
		*/
		/*
			데이터타입 선언
			참조변소 s0을 선언
			문자열 "abc"값으로 초기화
		*/	
		String s0 = "abc";
		String s1 = s0.toUpperCase();

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

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

	} // end of main()

} // end of Exam_For_4 class

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

  • Exam_For_5
package a.b.c.ch1;

public class Exam_For_5 {

	public static void main(String args[]) {
		
		String s2 = "A";

		char ch_L = s2.charAt(0);

		for (int i=ch_L; i < (ch_L + 26); i++) {
			System.out.print((char)i + " ");
		}
		System.out.println();

		// public String toLowerCase()
		String s3 = s2.toLowerCase();

		char ch_S = s3.charAt(0);

		for (int i=ch_S; i < (ch_S + 26); i++) {
			System.out.print((char)i + " ");
		}

	} // end of main()

} // end of Exam_For_5 class
  • Exam_For_5 ASCII 코드 2진수 8진수 16진수 출력하기
package a.b.c.ch1;

public class Exam_For_5 {

	public static void main(String args[]) {
		
		// String s = "abcdefghijklmnopqrstuvwxyz";
		// char cc = new char[26];

		String s2 = "A";

		char ch_L = s2.charAt(0);

		for (int i=ch_L; i < (ch_L + 26); i++) {
			System.out.print((char)i + " ");

			System.out.print(i + " ");
			// 2진수 0B, 0b
			System.out.print(Integer.toBinaryString(i) + " ");
			// 8진수 0
			System.out.print("0" + Integer.toOctalString(i) + " ");
			// 16진수 0x, 0X
			System.out.print("0x" + Integer.toHexString(i) + " ");
			System.out.print("0X" + Integer.toHexString(i) + " ");
			System.out.println();
		}
		System.out.println();

		// public String toLowerCase()
		String s3 = s2.toLowerCase();

		char ch_S = s3.charAt(0);

		for (int i=ch_S; i < (ch_S + 26); i++) {
			System.out.print((char)i + " ");

			System.out.print(i + " ");
			System.out.print(Integer.toBinaryString(i) + " ");
			System.out.print("0" + Integer.toOctalString(i) + " ");
			System.out.print("0x" + Integer.toHexString(i) + " ");
			System.out.print("0X" + Integer.toHexString(i) + " ");
			System.out.println();
		}

	} // end of main()

} // end of Exam_For_5 class

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

  • 16진수 출력 짜보기 - 내가 짠 코드
package a.b.c.ch1;

/*
하기	main() 함수에 초기화 된 지역변수 5개를 16진수로 콘솔에 출력하시오 
단:	함수를 만들어서 main() 함수에서 각 함수를 호출해서 실행 시키시오
	콘솔에 16진수를 출력할 때는 0x 를 붙여서 출력하시오
함수 이름 toHex_S0(), toHex_S1(), toHex_S2(), toHex_S3(), toHex_S4()
*/

public class Exam_For_6_test {

	public void toHex_S0(String s) {

		for (int i = 0; i < s.length(); i++) {
			System.out.print(s.charAt(i) + " "); // 인덱스로 한 글자씩 빼기
			System.out.println("0x" + Integer.toHexString(s.charAt(i)) + " "); // 한 글자의 16진수
		}
		System.out.println();
	}
		
	public void toHex_S1(String s) {
		new Exam_For_6_test().toHex_S0(s);
	}
	
	public void toHex_S2(String s) {
		new Exam_For_6_test().toHex_S0(s);
	}
	
	public void toHex_S3(String s) {
		new Exam_For_6_test().toHex_S0(s);	
	}

	public void toHex_S4(String s) {
		new Exam_For_6_test().toHex_S0(s);
	}
		
	public static void main(String args[]) {
		
		// 영문자 소문자
		String s0 = "abcdefghijklmnopqrstuvwxyz";	
		// 영문자 대문자
		String s1 = s0.toUpperCase();	
		// 숫자
		String s2 = "0123456789";
		// 연산기호
		String s3 = "*+-/";
		// 특수 문자
		String s4 = "!@#%%^&*";

		new Exam_For_6_test().toHex_S0(s0);
		new Exam_For_6_test().toHex_S1(s1);
		new Exam_For_6_test().toHex_S2(s2);
		new Exam_For_6_test().toHex_S3(s3);
		new Exam_For_6_test().toHex_S4(s4);

	} // end of main()

} // end of Exam_For_6_test class
  • 선생님이 주신 코드 - 함수 5개 만들기
package a.b.c.ch1;

/*
하기	main() 함수에 초기화 된 지역변수 5개를 16진수로 콘솔에 출력하시오 
단:	함수를 만들어서 main() 함수에서 각 함수를 호출해서 실행 시키시오
	콘솔에 16진수를 출력할 때는 0x 를 붙여서 출력하시오
함수 이름 toHex_S0(), toHex_S1(), toHex_S2(), toHex_S3(), toHex_S4()

16:45 까지 하기 
*/
public class Exam_For_6 {

	public void toHex_S0(String str) {
		System.out.println("Exam_For_6.toHex_S0() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}

	public void toHex_S1(String str) {
		System.out.println("Exam_For_6.toHex_S1() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}

	public void toHex_S2(String str) {
		System.out.println("Exam_For_6.toHex_S2() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}

	public void toHex_S3(String str) {
		System.out.println("Exam_For_6.toHex_S3() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}

	public void toHex_S4(String str) {
		System.out.println("Exam_For_6.toHex_S4() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}

	public static void main(String args[]) {
		
		// 영문자 소문자
		String s0 = "abcdefghijklmnopqrstuvwxyz";
		// 영문자 대문자 
		String s1 = s0.toUpperCase();
		// 숫자 
		String s2 = "0123456789";
		// 연산기호
		String s3 = "*+-/";
		// 특수 문자
		String s4 = "!@#%%^&*";

		new Exam_For_6().toHex_S0(s0);
		new Exam_For_6().toHex_S1(s1);
		new Exam_For_6().toHex_S2(s2);
		new Exam_For_6().toHex_S3(s3);
		new Exam_For_6().toHex_S4(s4);

	}
}
  • 로직이 1개 필요하여 함수 1개 + 문자 입력을 받아 출력하고 싶은 문자열 출력
package a.b.c.ch1;

/*
하기	main() 함수에 초기화 된 지역변수 5개를 16진수로 콘솔에 출력하시오 
단:	함수를 만들어서 main() 함수에서 각 함수를 호출해서 실행 시키시오
	콘솔에 16진수를 출력할 때는 0x 를 붙여서 출력하시오
함수 이름 toHex_S0(), toHex_S1(), toHex_S2(), toHex_S3(), toHex_S4()

16:45 까지 하기 
*/
public class Exam_For_6_1 {

	public void toHex_Str(String str) {
		System.out.println("Exam_For_6.toHex_S0() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}
	
	public static void main(String args[]) {
		
		// 영문자 소문자
		String s0 = "abcdefghijklmnopqrstuvwxyz";
		// 영문자 대문자 
		String s1 = s0.toUpperCase();
		// 숫자 
		String s2 = "0123456789";
		// 연산기호
		String s3 = "*+-/";
		// 특수 문자
		String s4 = "!@#%%^&*";

		if (args.length == 1){

			String s = args[0];
			s = s.toUpperCase();

			if ("A".equals(s)){
				new Exam_For_6_1().toHex_Str(s0);
			}
			if ("B".equals(s)){
				new Exam_For_6_1().toHex_Str(s1);
			}
			if ("C".equals(s)){
				new Exam_For_6_1().toHex_Str(s2);
			}
			if ("D".equals(s)){
				new Exam_For_6_1().toHex_Str(s3);
			}
			if ("E".equals(s)){
				new Exam_For_6_1().toHex_Str(s4);
			}
		}else{
			System.out.println(   "A : 영문자 소문자 \n"
								+ "B : 영문자 대문자 \n"
								+ "C : 숫자 \n"
								+ "D : 연산 기호 \n"
								+ "E : 특수 문자 \n"
								+ "를 입력하시오 ");

		}
	}
}
  • 숫자 입력을 받아 출력하고 싶은 문자열 출력
package a.b.c.ch1;

import java.util.Scanner;

/*
하기	main() 함수에 초기화 된 지역변수 5개를 16진수로 콘솔에 출력하시오 
단:	함수를 만들어서 main() 함수에서 각 함수를 호출해서 실행 시키시오
	콘솔에 16진수를 출력할 때는 0x 를 붙여서 출력하시오
함수 이름 toHex_S0(), toHex_S1(), toHex_S2(), toHex_S3(), toHex_S4()

16:45 까지 하기 
*/

public class Exam_For_6_2 {

	public void toHex_Str(String str) {
		System.out.println("Exam_For_6.toHex_Str() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}
	
	public static void main(String args[]) {
		
		// 영문자 소문자
		String s0 = "abcdefghijklmnopqrstuvwxyz";
		// 영문자 대문자 
		String s1 = s0.toUpperCase();
		// 숫자 
		String s2 = "0123456789";
		// 연산기호
		String s3 = "*+-/";
		// 특수 문자
		String s4 = "!@#%%^&*";

		System.out.println(   "1 : 영문자 소문자 \n"
							+ "2 : 영문자 대문자 \n"
							+ "3 : 숫자 \n"
							+ "4 : 연산 기호 \n"
							+ "5 : 특수 문자 \n"
							+ "를 입력하시오 ");
		
		Scanner sc = new Scanner(System.in);
		int iVal = sc.nextInt();

		if (1 == iVal){
			new Exam_For_6_2().toHex_Str(s0);
		}
		if (2 == iVal){
			new Exam_For_6_2().toHex_Str(s1);
		}
		if (3 == iVal){
			new Exam_For_6_2().toHex_Str(s2);
		}
		if (4 == iVal){
			new Exam_For_6_2().toHex_Str(s3);
		}
		if (5 == iVal){
			new Exam_For_6_2().toHex_Str(s4);
		}
	}
}

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

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

/*
하기	main() 함수에 초기화 된 지역변수 5개를 16진수로 콘솔에 출력하시오 
단:	함수를 만들어서 main() 함수에서 각 함수를 호출해서 실행 시키시오
	콘솔에 16진수를 출력할 때는 0x 를 붙여서 출력하시오
함수 이름 toHex_S0(), toHex_S1(), toHex_S2(), toHex_S3(), toHex_S4()

16:45 까지 하기 
*/
public class Exam_For_6 {

	// toHex_S0 함수를 선언한다.
	// 매개변수는 String 문자열을 받는다.
	public void toHex_S0(String str) {
		// toHex_S0 함수를 호출하면 제일 먼저 하기 로그가 콘솔에 출력된다.
		System.out.println("Exam_For_6.toHex_S0() 함수 진입 >>> : ");

		// toHex_S0 함수 블럭에서 매개변수 선언한  String 클래스 str 변수는 
		// toHex_S0 함수 블럭에서 사용이 가능하다.

		// 메인 함수에 new Exam_For_6().toHex_S0(s0); 이렇게 
		// toHex_S0(s0) 함수를 호출하면 
		// 인수로 넘어온 s0 변수의 값이
		// str 변수에 대입이 된다. String str = s0
		// String s0 = "abcdefghijklmnopqrstuvwxyz"; 초기화 된 값이
		// str = "abcdefghijklmnopqrstuvwxyz"; 값이 대입된다.

		// 이상태에서 str 에는 "abcdefghijklmnopqrstuvwxyz" 이렇게 영문자 소문자 26개 대입되어 있다.

		// if  문에서 
		// str 의 값이 null 이 아니고 길이가 0 보다 클 때만 if  문이 수행된다.
		// str 에 "abcdefghijklmnopqrstuvwxyz" 값이 있기 때문에
		// if  문 블럭이 수행될 수 있다. 
		if (str !=null && str.length() > 0){
			// if 문 블럭이 수행이 되면 
			
			// 제일 먼저 char 데이터 타입이 c 지역변수 선언되어서 ' ' 초기화 된다.
			char c = ' ';

			// for  문이 str 문자열 길이 만큼(영문자 소문자 26개 만큼) 반복 수행(루프 가 돈다)된다.
			for (int i=0; i < str.length(); i++ ){

				// for 문 첫번째 라인에서 
				// 변수 c 에 str 에 대입 된 영문자 소문자 26개가 순차적으로 대입이 된다.
				// str.charAt(int index) 함수에 의해서 
				c = str.charAt(i);

				// 콘솔에 c 변수에 대입된 영문자 소문자를  출력한다.
				System.out.print(c + " ");

				// 콘솔에 문자열 0x 와 영문자 소문자를 16진수 변환 값을 더해서 출력한다.
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}

	public void toHex_S1(String str) {
		System.out.println("Exam_For_6.toHex_S1() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}

	public void toHex_S2(String str) {
		System.out.println("Exam_For_6.toHex_S2() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}

	public void toHex_S3(String str) {
		System.out.println("Exam_For_6.toHex_S3() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}

	public void toHex_S4(String str) {
		System.out.println("Exam_For_6.toHex_S4() 함수 진입 >>> : ");

		if (str !=null && str.length() > 0){
			char c = ' ';
			for (int i=0; i < str.length(); i++ ){
				c = str.charAt(i);
				System.out.print(c + " ");
				System.out.print("0x" + Integer.toHexString(c) + " ");
				System.out.println();
			}
		}
	}

	public static void main(String args[]) {
		
		// String 클래스를 지역변수로 값을 초기화 했다.
		// 지역변수 s0, s1, s2, s3, s4 로 선언했다.
		// 각 변수에 문자열을 초기화 한다.
		// 문자열이 영문자 소문자
		// 영문자 소문자를 String 클래스의 toUpperCase() 함수로 대문자로 만든다.
		// 숫자 0 ~ 9
		// 연산자 : 사칙연산자 
		// 특수 문자 8개

		// 영문자 소문자
		String s0 = "abcdefghijklmnopqrstuvwxyz";
		// 영문자 대문자 
		String s1 = s0.toUpperCase();
		// 숫자 
		String s2 = "0123456789";
		// 연산기호
		String s3 = "*+-/";
		// 특수 문자
		String s4 = "!@#%%^&*";


		//  사용자 정의 Exam_For_6() 클래스를 인스턴스 해서 
		//	toHex_S0() 함수를 호출한다.
		//	인수(아규먼트로) s0 지역변수른 넘겨준다. 
		//	s0 지역변수에는 영문자 소문자 초기화 되어있다.
		new Exam_For_6().toHex_S0(s0);

		//  사용자 정의 Exam_For_6() 클래스를 인스턴스 해서 
		//	toHex_S1() 함수를 호출한다.
		//	인수(아규먼트로) s1 지역변수른 넘겨준다. 
		//	s1 지역변수에는 영문자 대문자가 초기화 되어있다.
		new Exam_For_6().toHex_S1(s1);

		//  사용자 정의 Exam_For_6() 클래스를 인스턴스 해서 
		//	toHex_S2() 함수를 호출한다.
		//	인수(아규먼트로) s2 지역변수른 넘겨준다. 
		//	s2 지역변수에는 숫자 0 ~ 9 초기화 되어있다.
		new Exam_For_6().toHex_S2(s2);

		//  사용자 정의 Exam_For_6() 클래스를 인스턴스 해서 
		//	toHex_S3() 함수를 호출한다.
		//	인수(아규먼트로) s3 지역변수른 넘겨준다. 
		//	s3 지역변수에는 사칙연산자가 초기화 되어있다.
		new Exam_For_6().toHex_S3(s3);

		//  사용자 정의 Exam_For_6() 클래스를 인스턴스 해서 
		//	toHex_S4() 함수를 호출한다.
		//	인수(아규먼트로) s4 지역변수른 넘겨준다. 
		//	s4 지역변수에는 특수 문자가 초기화 되어있다.
		new Exam_For_6().toHex_S4(s4);

	}
}

Notes


 

728x90